0
我已经得到了控制规范,看起来像这样测试实例,然后调用一个方法生成的对象上失败
describe ExportController do
describe 'GET index' do
target_params = {type:'a', filter: 'b'}
expect(DataFetcher).to receive(:new).with(target_params)
get :index
end
end
控制器看起来像这样
class ExportController < ApplicationController
def index
@fetcher = DataFetched.new(target_params)
...
end
end
如果我像这样运行规范,一切都很酷。然而,我想要做的事与所得DataFetcher对象
class ExportController < ApplicationController
def index
@fetcher = DataFetcher.new(target_params)
@list = @fetcher.fetch_list
...
end
end
现在,当我运行spec失败有没有方法错误
NoMethodError
undefined method 'fetch_list' for nil:NilClass
请告诉我与呢?事情是,当我通过我的实际应用程序打这个控制器时,它按预期工作。 rspec在幕后做了什么,以及我将如何正确设置它?
感谢你们
谢谢。当我以这种方式期望使用DataFetcher时,它是否转换为测试双精度? – Neil 2015-03-02 22:16:08
我不确定“it”是什么意思,但DataFetcher.new将在传递target_params时返回一个测试double。 – 2015-03-02 22:23:24