rspec 存根 current_company
我有一个 Rails 3 项目,我想在其中存储在会话变量中选择的当前公司。
我正在使用员工控制器规范,并且想暂时删除 current_company,因为我正在隔离员工新控制器操作的规范示例。
it "should call current_company" do
company = mock_model(Company, :id => "1")
controller.should_receive(:current_company).and_return(company)
get :new
end
这是我对员工控制器的新操作,
def new
@staff = Staff.new
@staff.company_id = current_company.id
end
我不断收到错误,
Failure/Error: get :new
NameError:
undefined local variable or method `current_company' for #<StaffsController:0x000000028d6ad8>
我也尝试将其删除,而不是使用 should_receive
controller.stub!(:current_company).and_return(company)
我收到相同的错误。
I have a Rails 3 project in which I want to store the current company selected in a session variable.
I'm working with the staff controller spec and would like to stub out current_company for now as I'm isolating my spec example for the staff new controller action.
it "should call current_company" do
company = mock_model(Company, :id => "1")
controller.should_receive(:current_company).and_return(company)
get :new
end
Here is my new action for the staff controller
def new
@staff = Staff.new
@staff.company_id = current_company.id
end
I keep getting error
Failure/Error: get :new
NameError:
undefined local variable or method `current_company' for #<StaffsController:0x000000028d6ad8>
I've also tried just stubbing it out instead of using should_receive
controller.stub!(:current_company).and_return(company)
I get the same error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的代码对我来说看起来很好,它应该可以工作。一定还有其他我们没有看到的问题。我注意到控制器名称是“StaffsController”——正确吗?仔细检查控制器的名称和相应的规格——它们应该是相同的。
Your code looks fine to me, it should work. There must be some other problem we are not seeing. I notice the controller name is "StaffsController" -- is that correct? Double-check the names of the controller and the corresponding spec -- they should be the same.
我认为它在“应该成功”的示例/测试中被轰炸了,所以我把我的存根放在了之前的块中。
这适用于:
I think it was bombing out on the 'should be successful' example/test, so I've put my stubbing in a before block.
This works for: