2015-10-17 64 views
5

Variants这个问题已被询问多次,但他们大多与摩卡处理,我没有使用它。我是一个新手,所以这可能看起来微不足道,但我无法在我的spec文件中使用模拟(对于名为竞争对手的控制器)。NoMethodError:未定义的方法`模拟'的#<RSpec :: ExampleGroups :: CompetitionsController :: Create>

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 

我被困在模拟方法,所以我没有写多少其他东西。 我spec_helper文件具有以下内容

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 

我使用Ruby 2.2.1版本和4.2.1轨道

回答

8

使用代替模拟,这应该解决您的问题:

before :each do 
    @fake_c = double('Competition', :competition_id => 1, :competition_name => 'one', :competition_des => 'any') 
end 
+1

是的,它的确如此。模拟的使用是否被弃用? – Pukki

+0

显然,是的,'stub'和'mock'已被弃用。看到这个PR:https://github.com/rspec/rspec-mocks/pull/233/files –

+0

哦,是的,我忘了。谢谢 – Pukki

相关问题