我有一个Rails 3项目,我想在其中存储在会话变量中选择的当前公司。
我正在使用staff控制器规范,现在我想清除current_company,因为我正在为staff新控制器操作隔离我的规范示例。
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)我得到了同样的错误。
发布于 2011-02-23 09:21:09
我认为它在“应该成功”的例子/测试中失败了,所以我把我的存根放在了一个之前的代码块中。
require 'spec_helper'
describe StaffsController do
describe "GET 'new'" do
let(:staff) { mock_model(Staff, :company_id= => nil)}
let(:company) { mock_model(Company, :id => 1)}
before do
Staff.stub!(:new).and_return(staff)
controller.stub!(:current_company).and_return(company)
end
it "should be successful" do
get :new
response.should be_success
end
it "should call current_company" do
controller.should_receive(:current_company).and_return(company)
get :new
end
end
end这适用于:
class StaffsController < ApplicationController
def new
@staff = Staff.new
current_company.id
end
end发布于 2011-02-22 14:31:51
你的代码在我看来很好,它应该可以工作。肯定还有一些我们没有看到的其他问题。我注意到控制器的名称是"StaffsController“--对吗?仔细检查控制器的名称和相应的规范--它们应该是相同的。
https://stackoverflow.com/questions/5073588
复制相似问题