我正在使用带有 rspec 和 shoulda 的 Rails3。我有以下规格
describe PagesController, "on GET to show while logged off" do
before(:each) do
@site = Factory.create(:site)
@site.domains << Factory.create(:domain)
@site.save!
@site.pages << Factory.create(:page)
@site.menus << Factory.create(:menu, {:site=>@site, :is_visible=>true})
@site.menus << Factory.create(:menu, {:site=>@site, :is_visible=>true})
@site.menus << Factory.create(:menu, {:is_visible=>false, :site=>@site})
get :show
end
it { should render_template(:show) }
it { should render_template('layouts/2col') }
it { should assign_to(:site) }
it { should assign_to(:site).with(@site) }
it { should assign_to(:site).with(@site) }
it { should assign_to(:page).with(@site.pages[0])}
it "show visible menu_items only" do
assert assigns[:menu_items].length == 2
end
end
这是我的宝石文件
group :development, :test do
gem 'autotest'
gem 'factory_girl'
gem 'rspec', '>=2.0.0.beta.19'
gem 'rspec-rails', '>=2.0.0.beta.17'
gem 'shoulda'
end
这是我的 spec_helper
require 'rspec/rails'
require 'shoulda'
require 'shoulda/integrations/rspec2'
require 'authlogic/test_case'
require 'factory_girl
好的,到目前为止,一切都非常接近我以前看到的,但是每当我运行测试时,我都会收到如下错误
1) PagesController on GET to show while logged off
Failure/Error: it { should assign_to(:site) }
Expected action to assign a value for @site
# ./spec/controllers/pages_controller_spec.rb:19
不,我的第一个想法是代码被破坏了,但是应用程序运行正确。此外,如果我测试是否使用 assigns[:site] 分配了值,则测试通过。
有谁知道我需要改变什么才能使这些测试重新开始工作。
提前致谢
安迪