gpt4 book ai didi

ruby-on-rails - 未定义的方法 `mock_model'

转载 作者:行者123 更新时间:2023-12-04 05:51:49 26 4
gpt4 key购买 nike

我正在尝试将 Rspec 1.3.1 用于我在 2.3.8 上运行的 Rails 应用程序。我能够使用 stub_model 方法“ stub ”模型。但是当我调用 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 列表

 gem list rspec

*** LOCAL GEMS ***

rspec (1.3.1)
rspec-rails (1.3.3)

rails gem 列表

 gem list rails

*** LOCAL GEMS ***

rails (2.3.8, 2.3.5)

最佳答案

你对 mock_model 的调用是在你的 describe block 的顶层,这没有意义

你只能在 before(:each)、一个示例(即传递给 it 的 block 中)和类似的地方执行此操作

关于ruby-on-rails - 未定义的方法 `mock_model',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8572663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com