2

On my new Rails app I added a user model with scaffolding rails g scaffold User name email

It generated a bunch of tests that are failing. One of them is:

require 'spec_helper'

describe "users/new" do
  before(:each) do
    assign(:user, stub_model(User,
      :name => "MyString",
      :email => "MyString"
    ).as_new_record)
  end

  it "renders new user form" do
    render

    assert_select "form[action=?][method=?]", users_path, "post" do
      assert_select "input#user_name[name=?]", "user[name]"
      assert_select "input#user_email[name=?]", "user[email]"
    end
  end
end

My spec_helper.rb is:

require 'rubygems'
require 'spork'
require 'capybara/rspec'

Spork.prefork do

end

Spork.each_run do

end

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.mock_with :mocha
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
end

But I'm getting the error:

  2) users/new renders new user form
     Failure/Error: assign(:user, stub_model(User, NoMethodError:
       undefined method `stub' for #<User:0x57f0558>
     # ./spec/views/users/new.html.haml_spec.rb:5:in `block (2 levels) in <top (
required)>'

What's going on?

Manuel
  • 10,869
  • 14
  • 55
  • 86

1 Answers1

2

The problem is that your RSpec.configure block specifies config.mock_with :mocha, and the generated scaffold code stub_model is not compatible with mocha (which uses stubs and not stub as you find in RSpec code).

One option is to use a gem such as rspec-rails-mocha that ports stub_model to mocha; another is to mock with RSpec instead of mocha.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398