неопределенный метод `mock_model ' - PullRequest
2 голосов
/ 20 декабря 2011

Я пытаюсь использовать Rspec 1.3.1 для моего приложения rails, которое работает на 2.3.8. Я могу «заглушить» модели с помощью метода stub_model. Но когда я звоню mock_model, все идет не так, и я получаю трассировку стека

./spec/models/bucket_spec.rb:32: undefined method `mock_model' for Spec::Rails::Example::ModelExampleGroup::Subclass_2:Class (NoMethodError)
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/example/example_group_methods.rb:188:in `module_eval'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/example/example_group_methods.rb:188:in `subclass'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/example/example_group_methods.rb:55:in `describe'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/example/example_group_factory.rb:31:in `create_example_group'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/dsl/main.rb:28:in `describe'
from ./spec/models/bucket_test.rb:31
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/example_group_runner.rb:15:in `load'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/example_group_runner.rb:15:in `load_files'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/example_group_runner.rb:14:in `each'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/example_group_runner.rb:14:in `load_files'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/options.rb:134:in `run_examples'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/lib/spec/runner/command_line.rb:9:in `run'
from /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.1/bin/spec:5
from /usr/local/bin/spec:19:in `load'
from /usr/local/bin/spec:19

Файл bucket_spec.rb:

  require 'spec_helper'
   describe Bucket, "creation" do
  before(:each) do
    @bucket = stub_model(Bucket, :id => 1, :name => "Below Proficient", :color =>     "green", :min_range => 0, :max_range => 30, :class_group_id => 1).as_new_record
  end
  it "should be valid with all the attributes set to some randowm values" do
    @bucket.should be_valid
  end
  it "should be valid without min_range" do
    @bucket.min_range = nil
    @bucket.should be_valid
   end
  it "should be valid without max_range" do
    @bucket.max_range = nil
    @bucket.should be_valid
  end
  it "should be valid without class_group_id" do
    @bucket.class_group_id = nil
    @bucket.should be_valid
  end
  it "should not be valid without color" do
    @bucket.color = nil
    @bucket.should_not be_valid
  end
  it "should not be valid without name" do
    @bucket.name = nil
    @bucket.should_not be_valid
  end
end

describe Bucket, "saving" do
  @bucket = mock_model(Bucket)
  @bucket.should be_valid
end

Файл spec_helper.rb:

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

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
#
#RSpec.configure do |config|
#  # == Mock Framework
#  #
#  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#  #
#  # config.mock_with :mocha
#  # config.mock_with :flexmock
#  # config.mock_with :rr
#  config.mock_with :rspec
#
#  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
#  config.fixture_path = "#{::Rails.root}/spec/fixtures"
#
#  # If you're not using ActiveRecord, or you'd prefer not to run each of your
#  # examples within a transaction, remove the following line or assign false
#  # instead of true.
#  config.use_transactional_fixtures = true
#end
Spec::Runner.configure do |config|
  config.mock_with :rspec
  config.use_transactional_fixtures = true
end

Список драгоценных камней rspec

 gem list rspec

 *** LOCAL GEMS ***

 rspec (1.3.1)
 rspec-rails (1.3.3)

Список рельсов драгоценных камней

 gem list rails

 *** LOCAL GEMS ***

 rails (2.3.8, 2.3.5)

1 Ответ

7 голосов
/ 20 декабря 2011

Ваш звонок на mock_model находится на верхнем уровне вашего блока описания, который не имеет смысла

Вы можете сделать это только внутри before (: each), примера (т.е. в блоке, переданном в it) и подобных местах

...