Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: RSpec::Mocks::Mock#to_hash should return Hash #51

Closed
lucasrenan opened this issue Apr 13, 2011 · 10 comments
Closed

TypeError: RSpec::Mocks::Mock#to_hash should return Hash #51

lucasrenan opened this issue Apr 13, 2011 · 10 comments

Comments

@lucasrenan
Copy link

controller

def create
  @city = City.new(params[:city])

  respond_with do |format|
    if @city.save
      format.html { redirect_to(admin_city_path(@city), :notice => 'City was successfully created.') }
    else
      format.html { render :action => "new" }
    end
  end
end

spec

def mock_city(stubs={})
  @mock_city ||= mock_model(City, stubs).as_null_object
end

describe "with valid params" do
  it "assigns a newly created city as @city" do
    City.stub(:new).with({'these' => 'params'}) { mock_city(:save => true) }
    post :create, :city => {'these' => 'params'}
    assigns(:city).should be(mock_city)
  end

  it "redirects to the created city" do
    City.stub(:new) { mock_city(:save => true) }
    post :create, :city => {}
    response.should redirect_to(city_url(mock_city))
  end
end

The problem seems to be with:
admin_city_path(@city)

Resulting in this error msg:
TypeError: RSpec::Mocks::Mock#to_hash should return Hash

@justinko
Copy link
Contributor

What does mock_city do? Can you paste that code?

@lucasrenan
Copy link
Author

I updated the issue, see the mock_city method above. (it was generated by scaffold)
My app uses CouchRest Model as orm, I tried with Mongoid and specs are running with no problems.

@justinko
Copy link
Contributor

@myronmarston
Copy link
Member

Can you paste a backtrace?

@lucasrenan
Copy link
Author

exactly the same error

@alindeman
Copy link
Contributor

It seems that something is calling #to_hash ... maybe something within the ORM?

Because you've created a mock with #as_null_object, the mock will return self because you haven't explicitly stubbed it out.

A backtrace should help figure out what part of the system is calling #to_hash.

@alindeman
Copy link
Contributor

Another thing you could do is remove the #as_null_object bit so the mock will immediately raise an error when a method it doesn't recognize is called (in this case, #to_hash).

@lucasrenan
Copy link
Author

backtrace:

rake aborted!
ruby -S bundle exec rspec ./spec/controllers/admin/cities_controller_spec.rb failed
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@ozzieboard/gems/rspec-core-2.5.1/lib/rspec/core/rake_task.rb:139:in 'initialize'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:1112:in 'verbose'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@ozzieboard/gems/rspec-core-2.5.1/lib/rspec/core/rake_task.rb:131:in 'send'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@ozzieboard/gems/rspec-core-2.5.1/lib/rspec/core/rake_task.rb:131:in 'initialize'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:636:in 'call'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:636:in 'execute'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:631:in 'each'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:631:in 'execute'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:597:in 'invoke_with_call_chain'
/Users/lucasrenan/.rvm/rubies/ruby-1.8.7-p174/lib/ruby/1.8/monitor.rb:242:in 'synchronize'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:590:in 'invoke_with_call_chain'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:583:in 'invoke'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:2051:in 'invoke_task'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:2029:in 'top_level'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:2029:in 'each'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:2029:in 'top_level'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:2068:in 'standard_exception_handling'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:2023:in 'top_level'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:2001:in 'run'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:2068:in 'standard_exception_handling'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/lib/rake.rb:1998:in 'run'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/gems/rake-0.8.7/bin/rake:31
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/bin/rake:19:in 'load'
/Users/lucasrenan/.rvm/gems/ruby-1.8.7-p174@global/bin/rake:19

@lucasrenan
Copy link
Author

I removed #as_null_object then:

Failure/Error: post :create, :city => {'name' => 'sao paulo'} Mock "City_1005" received unexpected message :extractable_options? with (no args)

@dchelimsky
Copy link
Contributor

mock_model (part of rspec-rails, not rspec-mocks) supports code that interacts with a model via the ActiveModel API. The problem with satisfying general methods like to_hash is the question: what should be in the hash that it returns that would satisfy all potential uses?

Once as_null_object is removed, we see a call to extractable_options? which is not an ActiveModel method (or a core Ruby method), so mock_model should not be expected to support it.

I'm going to close this as I think supporting this is definitely out of scope for rspec-mocks and probably even out of scope of rspec-rails. If you want to continue the conversation in terms of what mock_model should or should not support, please submit a new issue to https://github.com/rspec/rspec-rails/issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants