我正在使用ruby" 2.0.0"和' rails','〜> 4.0.0'和' rspec-rails'," 2.14.2" (引自Gemfile。我有两个完全不同的应用程序,我在两者上都得到了相同的错误。
我使用脚手架生成器作为我的控制器。所以它也产生了我的控制器测试。特别是更新行动马上就开始了。 测试代码
describe "PUT update" do
describe "with valid params" do
it "updates the requested product" do
product = Product.create! valid_attributes
# Assuming there are no other products in the database, this
# specifies that the Product created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
expect_any_instance_of(Product).to receive(:update).with({ "product_name" => "Product 1" })
put :update, {:id => product.to_param, :product => { "product_name" => "Product 1" }}, valid_session
end
控制器代码:
# PATCH/PUT /products/1
#PATCH / PUT /products/1.json
def更新 respond_to do | format | if @ product.update_attributes(product_params) format.html {redirect_to @product,注意:'产品已成功更新。' } format.json {head:no_content} 其他 format.html {render action:" edit" } format.json {render json:@ product.errors,status :: unprocessable_entity} 结束 结束 端
所以我运行我的测试并且:
1) ProductsController PUT update with valid params updates the requested product
Failure/Error: Unable to find matching line from backtrace
Exactly one instance should have received the following message(s) but didn't: update
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-mocks-3.0.2/lib/rspec/mocks/any_instance/recorder.rb:92:in `verify'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-mocks-3.0.2/lib/rspec/mocks/space.rb:69:in `block in verify_all'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-mocks-3.0.2/lib/rspec/mocks/space.rb:69:in `each_value'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-mocks-3.0.2/lib/rspec/mocks/space.rb:69:in `verify_all'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-mocks-3.0.2/lib/rspec/mocks.rb:45:in `verify'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/mocking_adapters/rspec.rb:21:in `verify_mocks_for_rspec'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:354:in `verify_mocks'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:345:in `run_after_example'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:163:in `block in run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `call'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `block (2 levels) in <class:Procsy>'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-rails-3.0.1/lib/rspec/rails/example/controller_example_group.rb:174:in `block (2 levels) in <module:ControllerExampleGroup>'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:294:in `instance_exec'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:294:in `instance_exec'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/hooks.rb:430:in `block (2 levels) in run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `call'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `block (2 levels) in <class:Procsy>'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-rails-3.0.1/lib/rspec/rails/adapters.rb:68:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:294:in `instance_exec'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:294:in `instance_exec'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/hooks.rb:430:in `block (2 levels) in run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `call'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `block (2 levels) in <class:Procsy>'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/hooks.rb:432:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/hooks.rb:485:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:303:in `with_around_example_hooks'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:145:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:494:in `block in run_examples'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:490:in `map'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:490:in `run_examples'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:457:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `block in run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `map'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `block in run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `map'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:112:in `block (2 levels) in run_specs'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:112:in `map'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:112:in `block in run_specs'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/reporter.rb:54:in `report'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:108:in `run_specs'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:86:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:70:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:38:in `invoke'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/exe/rspec:4:in `<top (required)>'
# /Users//.rvm/gems/ruby-2.0.0-p353/bin/rspec:23:in `load'
# /Users//.rvm/gems/ruby-2.0.0-p353/bin/rspec:23:in `<main>'
# /Users//.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
# /Users//.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'
#
# Showing full backtrace because every line was filtered out.
# See docs for RSpec::Configuration#backtrace_exclusion_patterns and
# RSpec::Configuration#backtrace_inclusion_patterns for more information.
在0.27911秒内完成(文件加载时间为1.85秒) 16个例子,1个失败
失败的例子:
rspec ./spec/controllers/products_controller_spec.rb:90#ProductsController PUT update with valid params更新请求的产品
我在此之后发布了回复,问题解决了:
答案 0 :(得分:0)
所以一位好朋友看了我的代码,他指出:
expect_any_instance_of(Product).to receive(:update).with({ "product_name" => "Product 1" })
正在尝试从模型接收更新。模型上没有更新方法:
class Product < ActiveRecord::Base
before_validation :normalize_name, on: :create
validates :product_name, presence: true, uniqueness: true
validates :product_cost, presence: true, :numericality => { :greater_than_or_equal_to => 1 }
protected
def normalize_name
self.product_name = self.product_name.downcase.titleize
end
end
因此,修复包括将:update_attributes添加到测试中以便调用模型,而不是像之前那样执行控制器操作。
expect_any_instance_of(Product).to receive(:update_attributes).with({ "product_name" => "Product 1" })
现在测试通过了。
我希望这会有所帮助。我对Rails相当新,所以请承担任何不准确或错误。随意报告拼写错误或更正,他们总是受欢迎。