开发者

assigns_to doesn't work for Rails 3 Shoulda on Ubuntu

开发者 https://www.devze.com 2023-01-16 23:19 出处:网络
I\'m using Rails3 with rspec and shoulda. I have the below spec describe PagesController, \"on GET to show while logged off\" do

I'm using Rails3 with rspec and shoulda. I have the below spec

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开发者_JAVA百科 == 2
  end
end

Here's my Gem File

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

and here's my spec_helper

require 'rspec/rails'
require 'shoulda'
require 'shoulda/integrations/rspec2'
require 'authlogic/test_case'
require 'factory_girl

Ok so far everything pretty close matches what I've seen before, however whenever I run my tests I get the errors like below

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

No my first thought was that the code was broken, however the application runs correcty. Also if I test that the values are assigned by using the assigns[:site] then the test passes.

Has anyone any idea what I need to change in order to make these tests start working again.

Thanks In Advance

Andy


You need to call subject { controller } before your it statements. This actually confused me so badly for a while that I wrote my first ever blog post about it.


If you are using Ruby 1.9.2 the assign_to matcher with the shoulda-matchers gem version lower than 1.0.0beta2 will still not work, even if you include the subject { controller } (which, I believe, is not really needed).

It's caused by a change in Ruby 1.9.2. Here is the bugreport for shoulda. The fix is already included and released in shoulda-matchers version 1.0.0beta2.

So just have this in your Gemfile:

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

and update to the latest version (1.0.0.beta2 atm):

bundle update shoulda-matchers
0

精彩评论

暂无评论...
验证码 换一张
取 消