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

これが私のGemファイルです

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のバグレポートは次のとおりです。修正は既に含まれてshoulda-matchersおり、バージョン1.0.0beta2でリリースされています。

だからあなたの中にこれを入れてくださいGemfile

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

最新バージョン (1.0.0.beta2 atm) に更新します。

bundle update shoulda-matchers
于 2011-03-17T15:54:17.333 に答える