问题描述
我一直在尝试深入研究 RSpec 2,但其自动生成的控制器规范不适用于任何版本的 RSpec 2 和任何版本的 Ruby 或任何版本的 Rails.也许我遗漏了一些明显的东西?
I have been attempting to dive into RSpec 2 but its auto generated controller specs do not work for any version of RSpec 2 with any version of Ruby or any version of Rails. Maybe I'm missing something obvious?
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
?
Why is this mock/stub returning nil
?
更新
这是来自我的控制器的 show 方法:
This is from my controller's show method :
def show
@category = Category.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @category }
end
end
谢谢!
推荐答案
RSpec 在 Rails3 beta 和 RSpec 2 beta.10 到 Rails3 版本和 RSpec 2 beta.20 之间有一些严重的冲突.
RSpec has some serious conflicts between Rails3 beta, and RSpec 2 beta.10 to Rails3 release, and RSpec 2 beta.20.
我尝试复制和粘贴脚手架之间的差异,但我通过删除所有规格并重新生成它们来完全清理现场.卸载 haml
,并且只安装 Rails 3 的 haml-rails
.
I tried copying and pasting the differences between the scaffolds, but I cleared up the sitation entirely by deleting all the specs, and regenerating them. Uninstalling haml
, and installing only haml-rails
for rails 3.
现在运行所有规范.
这篇关于有人可以向我描述 RSpec 2 在这方面的作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!