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
將返回:任何人都知道我爲什麼會收到此RSpec錯誤?獲得零< -
1) CategoriesController GET show assigns the requested category as @category
Failure/Error: assigns(:category).should be(mock_category)
expected Category_1002, got nil
我很困惑在這裏,因爲這是一個正確的是RSpec的設置框控制器。爲什麼這可能會失敗?
我的版本:
Rails 3.0.0.beta4
Ruby 1.8.7
RSpec 2.0.0.beta.10
也試過這個,完全相同的重複性誤差:
Rails 3.0.0
Ruby 1.8.7
RSpec 2.0.0.beta.20
我用來生成規格爲rails g scaffold Category
在我的application.rb中的命令
config.generators do |g|
g.template_engine :haml
g.test_framework :rspec, :fixture => true, :views => false
end
UPDATE
這適用於Rails 3和RSpec2的任何腳手架控制器。它保證失敗。任何人都知道這應該寫成什麼?
哇!我無法告訴你我是多麼榮幸能夠得到你的回答。我是你工作的忠實粉絲。我上面更新了我的答案。 – Trip 2010-09-02 14:32:30