【发布时间】:2010-09-03 16:48:08
【问题描述】:
我一直在尝试深入研究 RSpec 2,但其自动生成的控制器规范不适用于任何版本的 RSpec 2 与任何版本的 Ruby 或任何版本的 Rails。也许我遗漏了一些明显的东西?
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 ?
更新
这是来自我的控制器的 show 方法:
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做了什么,也不知道它为什么会出现,但这似乎不应该出现。尝试删除它。 -
能把index方法的内容贴出来吗?
-
@zetetic,我是个白痴,我发布了错误的示例。上面的只是略有不同。有什么你想从中看到的方法吗?
-
@rspeicher,也不完全确定它的作用,但它是否存在并没有什么不同。
标签: ruby-on-rails testing ruby-on-rails-3 rspec rspec2