我一直试图潜入RSpec 2,但其自动生成的控制器规范不适用于任何版本的Ruby或任何版本的Rails的RSpec 2的任何版本。也许我错过了一些明显的东西?有人可以向我描述RSpec 2在这方面做了什么?
def mock_category(stubs={})
@mock_category ||= mock_model(Category, stubs).as_null_object
end
describe "GET show" do
it "assigns the requested category as @category" do
Category.stub(:find).with("37") { mock_category }
get :show, :id => "37"
assigns(:category).should be(mock_category)
end
end
这是从rails g scaffold Category
RSpec的自动生成返回此:
Failures:
1) CategoriesController GET show assigns the requested category as @category
Failure/Error: assigns(:category).should be(mock_category)
expected Category_1002, got nil
# ./spec/controllers/categories_controller_spec.rb:21
# /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:239:in `inject'
这是为什么模拟/存根返回nil
?
更新
这是我的控制器的显示方法:
def show
@category = Category.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @category }
end
end
谢谢!
我不知道'.as_null_object'确实存在,或者它为什么存在,但是这看起来应该不存在。尝试删除它。 – 2010-09-03 18:07:06
你可以发布索引方法的内容吗? – zetetic 2010-09-03 18:17:35
@zetetic,我是个白痴,我发布了错误的例子。上面的只是稍有不同。你想从中看到什么方法吗? – Trip 2010-09-03 18:30:04