我是rails的新手,正在尝试使用rspec测试控制器。我的第一个测试是在调用show操作时,它应该通过url查找类别。
问题是当我添加桩代码时,我会收到以下错误:
undefined method `find' for #
我的测试看起来像这样:
require 'spec_helper'
describe CategoriesController do
describe "GET /category-name/" do
before(:each) do
@category = mock_model(Category)
Category.stub!(:find).with(:first, :conditions => ["url = :url", {:url => "category-name"}]).and_return(@category)
end
it "should find the category by url" do
controller.show
Category.should_receive(:find).with(:first, :conditions => ["url = :url", {:url => "category-name"}]).and_return(@category)
end
end
end