0
votes

I am using ruby "2.0.0" and 'rails', '~> 4.0.0' and 'rspec-rails', "2.14.2" ( quoted from the Gemfile. I have two entirely different apps and I am getting the same error on both.

I used the scaffold generator for my controller. So it also generated my controller tests. In particular the update action broke right away. Test code

  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

Controller code:

 # PATCH/PUT /products/1

# PATCH/PUT /products/1.json

def update respond_to do |format| if @product.update_attributes(product_params) format.html { redirect_to @product, notice: 'Product was successfully updated.' } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @product.errors, status: :unprocessable_entity } end end end

So i run my test and:

  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.

Finished in 0.27911 seconds (files took 1.85 seconds to load) 16 examples, 1 failure

Failed examples:

rspec ./spec/controllers/products_controller_spec.rb:90 # ProductsController PUT update with valid params updates the requested product

I am posting the response after this, the problem is solved:

1
So a good friend took a glance at my code and he pointed out that: expect_any_instance_of(Product).to receive(:update).with({ "product_name" => "Product 1" }) So the fix consists in adding :update_attributes to the tests in order to make the call to the model, instead as a controller action as it was doing before. So: expect_any_instance_of(Product).to receive(:update_attributes).with({ "product_name" => "Product 1" }) And now the test pass. I hope this helps. I am fairly new to Rails, so please bear with any accuracies or errors. Feel free to report typos or corrections. Thank you all.Joseworks

1 Answers

0
votes

So a good friend took a glance at my code and he pointed out that:

expect_any_instance_of(Product).to receive(:update).with({ "product_name" => "Product 1" })

Is trying to receive an update from the model. There is no update method on the model:

    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

So the fix consists in adding :update_attributes to the tests in order to make the call to the model, instead as a controller action as it was doing before.

        expect_any_instance_of(Product).to receive(:update_attributes).with({ "product_name" => "Product 1" })

And now the test pass.

I hope this helps. I am fairly new to Rails, so please bear with any inaccuracies or errors. Feel free to report typos or corrections, they are always welcome.