thoughtbot / shoulda-matchers

Simple one-liner tests for common Rails functionality
https://matchers.shoulda.io
MIT License
3.52k stars 911 forks source link

undefined method `name' for Controller rather than failure message when testing with use_before_filter #484

Closed shedd closed 10 years ago

shedd commented 10 years ago

Am seeing an error when testing with use_before_filter in 2.6.0 rather than a failure message.

With this controller:

class Api::V1::DinersController < ApplicationController
  before_action :set_diner, only: [:show]

  def show
    ...
  end

end

And this test:

describe Api::V1::DinersController do
  describe 'GET show' do
    it { should use_before_filter(:set_order) }
  end
end

I'm getting this error:

1) Api::V1::DinersController GET show should have :set_order as a before_filter
   Failure/Error: it { should use_before_filter(:set_order) }
   NoMethodError:
     undefined method `name' for #<Api::V1::DinersController:0x0000000f1c1858>
   # /usr/local/rvm/gems/ruby-2.1.1/gems/shoulda-matchers-2.6.0/lib/shoulda/matchers/action_controller/callback_matcher.rb:77:in `failure_message'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-expectations-2.14.5/lib/rspec/expectations/handler.rb:30:in `handle_matcher'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/memoized_helpers.rb:68:in `should'
   # ./spec/controllers/api/v1/diners_controller_spec.rb:19:in `block (3 levels) in <top (required)>'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:114:in `instance_eval'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:114:in `block in run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:179:in `call'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:179:in `run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:108:in `call'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:108:in `run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:446:in `run_hook'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:340:in `run_around_each_hooks'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:111:in `run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:390:in `block in run_examples'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `map'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `run_examples'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:371:in `run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `block in run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `map'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `map'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block in run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/reporter.rb:58:in `report'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:25:in `run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
   # /usr/local/rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'

Note that the test is wrong - the method passed is incorrect. It should be show_diner but it is show_order.

When this is fixed, the test passes as expected.

Is this the expected failure message in this scenario?

mcmire commented 10 years ago

No, this is not expected. Thanks for the report, I'll have this fixed soon.

mcmire commented 10 years ago

@shedd I just released 2.6.1.rc1, can you update your Gemfile and see if your tests work again?

shedd commented 10 years ago

Things are looking good on this front with 2.6.1.rc1! Thanks!