在我的新Rails应用程序中,我添加了一个带脚手架rails g scaffold User name email
它产生了一堆失败的测试。其中之一是:
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
我的spec_helper.rb是:
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
但是我收到了错误:
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)>'
发生了什么事?
答案 0 :(得分:2)
问题是您的RSpec.configure
阻止指定了config.mock_with :mocha
,并且生成的支架代码stub_model
与mocha(使用stubs
而非stub
不兼容你在RSpec代码中找到了。)
一种选择是使用端口stub_model
的{{3}}等宝石来摩卡;另一个是用RSpec而不是mocha来模拟。