You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用rspec-page-regression时expect方法出现参数数量错误(给定1,预期0)

解决rspec-page-regression中match_expectation参数数量不符的问题

这个错误的根源是你对match_expectation的用法理解有误——它并不是一个RSpec匹配器,而是一个独立的断言方法,不需要包裹在expect(page).to语句里。

当你写expect(page).to match_expectation时,RSpec会自动把page作为参数传递给match_expectation方法,但这个方法本身设计为不需要参数(它默认会使用当前的page对象进行对比),所以就触发了wrong number of arguments的错误。

修复后的代码

只需要直接调用match_expectation即可,不需要expect包裹:

describe "my page", :type => :feature, :js => true do
  before(:each) do
    visit EnvConfig.get :app_url
  end
  it { match_expectation }
end

如果想要让测试用例的描述更清晰,也可以写成带块的形式:

describe "my page", :type => :feature, :js => true do
  before(:each) do
    visit EnvConfig.get :app_url
  end

  it "matches the page regression baseline" do
    match_expectation
  end
end

额外提示

如果需要区分不同场景的基准截图(比如同一页面的不同状态),你可以给match_expectation传递一个自定义名称参数:

match_expectation("my_page_logged_in_state")

这样gem会自动寻找对应名称的基准截图文件进行对比。

内容的提问来源于stack exchange,提问作者Dmytro Sotnikov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:50:42