Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoMethodError: undefined method `mock' for #<RSpec::ExampleGroups::CompetitionsController::Create>

Variants of this question have been asked multiple times but most of them deal with mocha and I am not using it. I am a newbie to rails so this may seem trivial but I am not able to do use mock in my spec file (which for a controller named competitions).

  require 'rails_helper'
  require 'spec_helper'

  describe CompetitionsController do
    before :each do
      @fake_c = mock(Competition, :competition_id => 1, :competition_name => 'one', :competition_des => 'any')
    end
    describe 'create' do
      it 'should create new competition' do
        #CompetitionsController.stub(:create).and_return(mock('Competition'))
        #post :create, {:id=>"1"}
      end
    end
  end

I am stuck at the mock method only so I have not written much else. My spec_helper file has the following contents

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

RSpec.configure do |config|
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|
    # This option will default to `true` in RSpec 4. It makes the `description`
    # and `failure_message` of custom matchers include text for helper methods
    # defined using `chain`, e.g.:
    #     be_bigger_than(2).and_smaller_than(4).description
    #     # => "be bigger than 2 and smaller than 4"
    # ...rather than:
    #     # => "be bigger than 2"
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  # rspec-mocks config goes here. You can use an alternate test double
  # library (such as bogus or mocha) by changing the `mock_with` option here.
  config.mock_with :rspec do |mocks|
    # Prevents you from mocking or stubbing a method that does not exist on
    # a real object. This is generally recommended, and will default to
    # `true` in RSpec 4.
    mocks.verify_partial_doubles = true
  end
end

I am using ruby version 2.2.1 and rails 4.2.1

like image 923
Pukki Avatar asked Oct 17 '15 03:10

Pukki


1 Answers

Use double instead of mock, that should fix your issue:

before :each do
  @fake_c = double('Competition', :competition_id => 1, :competition_name => 'one', :competition_des => 'any')
end
like image 106
K M Rakibul Islam Avatar answered Nov 11 '22 05:11

K M Rakibul Islam