因此,我在一个类中有一个方法如下:
def installation_backlog
Api::Dashboards::InstallationsBacklog.new(operational_district_id, officer_id).backlog_tasks
end
我想说明一下。因此,我只是编写了一个RSpec测试来对其进行如下测试:
it "should call a new instance of InstallationsBacklog with the backlog_tasks method" do
expect_any_instance_of(Api::Dashboards::InstallationsBacklog).to receive(:backlog_tasks)
@installation_officer.installation_backlog # @installation_officer is a new instance of the container class.
end
这正在工作。
但是,我开始怀疑这是否是正确的方法。就像:我确定即使我对错误的方法(可能不存在)进行测试并对其进行测试,它还会通过还是失败?
我尝试过了
因此,如果以后方法名称被更改,则该测试无法检测到该方法。
因此,这是问题:我如何确定代码中实际上存在RSpec stub 方法?
最佳答案
这是我的设置方法。可能有帮助..
let(:backlog) {
double('Backlog', backlog_tasks: [])
}
before do
allow(Api::Dashboards::InstallationsBacklog).to receive(:new).
and_return(backlog)
end
it 'instantiates InstallationBacklog' do
expect(Api::Dashboards::InstallationBacklog).to receive(:new).
with(operational_district_id, officer_id)
@installation_officer.installation_backlog
end
it 'calls backlog_tasks on instance of InstallationBacklog' do
expect(backlog).to receive(:backlog_tasks)
@installation_officer.installation_backlog
end
关于ruby-on-rails - 如何正确使用 stub 在rspec中进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35042831/