我有许多视图规范需要对某些方法进行存根。这是我认为可行的方法(在 spec_helper.rb 中):
Spec::Runner.configure do |config|
config.before(:each, :type => :views) do
template.stub!(:request_forgery_protection_token)
template.stub!(:form_authenticity_token)
end
end
但是当我运行任何视图规范时,它会失败
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.template
在每个示例的 before(:each) 块中执行完全相同的操作效果很好。