这是错误:
private method `desc' called for #<Array:0x0000010532e280>
规范:
describe SubjectsController do
before(:each) do
@subject = mock_model(Subject)
end
describe "#0002 - GET #index" do
before(:each) do
subjects = [@subject, mock_model(Subject), mock_model(Subject)]
Subject.stub!(:all).and_return(subjects)
Subject.all.stub!(:desc).and_return(subjects)
get :index
end
it { response.should be_success }
it { response.should render_template("index") }
end
end
和控制器:
def index
@subjects = Subject.all(conditions: {company_id: current_user.company.id}).desc(:created_at)
end
我不知道如何解决这个问题,有人可以帮个忙吗? 你能否就如何测试这种方法给我建议? THX。
答案 0 :(得分:4)
这有点嘲弄&amp;链接比我更喜欢,但你可以这样做:
subjects = [...]
desc_mock = double("desc order mock")
desc_mock.should_receive(:desc).with(:created_at).and_return(subjects)
conditions = {...}
Subject.should_receive(:all).with(conditions).and_return(desc_mock)
您还可以通过将查询移动到带有一些参数的命名范围来简化这一过程。然后,您的测试可以验证主题是否使用正确的参数收到您的范围,如:
Subject.should_receive(:user_company).with(current_user.id).and_return(subjects)
答案 1 :(得分:2)
stub_chain应该在这里工作:
Subject.stub_chain(:all,:desc) { [mock_model(Subject)]*2 }