Forum: RSpec undefined method `mock' for #<RSpec::Core::ExampleGroup::Nested...

Posted by Jan Limpens (Guest)
on 2010-10-04 06:04
(Received via mailing list)
Hekko,

what does the following mean?

1) PortfolioItemsController should render index
     Failure/Error: @portfolio = mock_model(Portfolio)
     undefined method `mock' for
#<RSpec::Core::ExampleGroup::Nested_1:0x0000010214bba0>
     # 
/Users/jan/.rvm/gems/ruby-1.9.2-p0@rails3/gems/actionpack-3.0.0/lib/action_dispatch/testing/assertions/routing.rb:177:in
`method_missing'
     # 
/Users/jan/.rvm/gems/ruby-1.9.2-p0@rails3/gems/rspec-rails-2.0.0.beta.22/lib/rspec/rails/mocks.rb:71:in
`mock_model'
     # ./spec/controllers/portfolio_items_controller_spec.rb:7:in `block 
(2
levels) in <top (required)>'

This is using mongoid, with rails 3

require 'spec_helper'

describe PortfolioItemsController do
  render_views

  before(:all) do
    @portfolio = mock_model(Portfolio) #dies here
    @item  = mock_model(PortfolioItem)
    @items = [@item, mock_model(PortfolioItem)]
    PortfolioItem.stub!(:find).and_return(@item)
    PortfolioItem.stub!(:all).and_return(@items)
    @portfolio.stub!(:items).and_return(@items)
  end

  it "should render index" do
    response.should be_success
  end

end

#routes.rb
  resources :portfolios do
    resources :portfolio_items do
      resources :images
    end
  end

class Portfolio
  include Mongoid::Document
  validates_presence_of :title
  field :title, :type => String
  embed_many :items, :class_name => PortfolioItem.name
end

class PortfolioItem
  include Mongoid::Document
  embedded_in :portfolio, :inverse_of => :items
  field(:title)
  field(:description)
  field(:date)
  embed_many(:images)

  validates_presence_of :title, :date

end

Any idea, what I am doing wrong here?
On another note, I think be_success does not exist anymore, right?
Posted by Dan F. (dan_f)
on 2011-01-19 21:54
Please see:
https://github.com/rspec/rspec-rails/issues/closed#issue/279

You can't create mocks in before(:all), but it will work in 
before(:each)
Posted by Wilker (Guest)
on 2011-01-19 22:49
(Received via mailing list)
Yeah, that is right, because mocks live on a space that is different for
each spec (the MockSpace), this is why you can't create it in before 
:all ;)
---
Wilker LĂșcio
http://about.me/wilkerlucio/bio
Kajabi Consultant
+55 81 82556600
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.