3

我正在使用带有 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] 分配了值,则测试通过。

有谁知道我需要改变什么才能使这些测试重新开始工作。

提前致谢

安迪

4

2 回答 2

3

你需要subject { controller }在你的it陈述之前打电话。这实际上让我很困惑一段时间,以至于我写了我的第一篇关于它的博客文章

于 2010-09-20T00:50:02.707 回答
0

如果您使用的是 Ruby 1.9.2 ,则gem 版本低于 1.0.0beta2的assign_to匹配器仍然无法工作,即使您包含了(我相信这并不是真正需要的)。shoulda-matcherssubject { controller }

这是由 Ruby 1.9.2 中的更改引起的。这是shoulda的错误报告。该修复程序已包含在1.0.0beta2shoulda-matchers版本中并已发布。

所以只要在你的Gemfile

group :development, :test do
  gem 'shoulda-matchers'
  ...    

并更新到最新版本(1.0.0.beta2 atm):

bundle update shoulda-matchers
于 2011-03-17T15:54:17.333 回答