So I was testing emails with RSpec and the email_spec gem and I keep coming across this error that leaves me with no real clue as to what is causing it.
I am very new to RSpec and testing in general. When I googled this the error I found one post that was similar to this error as well as a brief documentation page on the error (which came off to me as very ambiguous).
I believe the reason is due to a possibly outdated gem or method from email_spec, but I am just speculating (there's a pun for your time).
This is the error I keep receiving (I redacted my email for this post):
1) UserMailer it sends an activation email when the user signs up should be delivered from XXXXXXXX#gmail.com
Failure/Error: it { should deliver_from "XXXXXXXX#gmail.com" }
NoMethodError:
undefined method `addrs' for #<Mail::UnstructuredField:0x007fc5c680bf18>
# ./spec/mailers/user_mailer_spec.rb:12:in `block (3 levels) in <top (required)>'
Few things to point out:
I am not directly referencing the addrs method in any of my tests (so I believe)
I have included Email::Helpers and Email::Matchers in my config/test.rb file.
Here is my UserMailer test:
require "spec_helper"
require "email_spec"
describe UserMailer do
describe "it sends an activation email when the user signs up" do
user = FactoryGirl.build(:user)
subject { UserMailer.activate_account(user) }
it { should deliver_to user.email }
# Error seems to be occurring from the "deliver_from" method
it { should deliver_from "XXXXXXX2#gmail.com" }
it { should have_subject "Please confirm to activate your account." }
it { should have_body_text "Hi, #{user.first_name}" }
it { should have_body_text "Confirm Your Account" }
end
end
I would just like to be helped and pointed in the right direction as to what is causing this issue so I may take the appropriate steps to resolve it.
EDIT
Added the backtrace as requested.
# /Library/Ruby/Gems/2.0.0/gems/mail-2.6.3/lib/mail/field.rb:189:in `method_missing'
# /Library/Ruby/Gems/2.0.0/gems/email_spec-1.6.0/lib/email_spec/matchers.rb:92:in `block in matches?'
# /Library/Ruby/Gems/2.0.0/gems/email_spec-1.6.0/lib/email_spec/matchers.rb:6:in `address_array'
# /Library/Ruby/Gems/2.0.0/gems/email_spec-1.6.0/lib/email_spec/matchers.rb:92:in `matches?'
# /Library/Ruby/Gems/2.0.0/gems/rspec-expectations-2.99.2/lib/rspec/expectations/handler.rb:24:in `handle_matcher'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/memoized_helpers.rb:77:in `should'
# ./spec/mailers/user_mailer_spec.rb:12:in `block (3 levels) in <top (required)>'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:116:in `block in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:178:in `call'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:178:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:241:in `instance_eval_with_args'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:108:in `call'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:108:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:446:in `run_hook'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:463:in `run_around_each_hooks'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:250:in `with_around_each_hooks'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:113:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:515:in `block in run_examples'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:511:in `map'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:511:in `run_examples'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:496:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:497:in `block in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:497:in `map'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:497:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:24:in `block (2 levels) in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:24:in `map'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:24:in `block in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/reporter.rb:58:in `report'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:21:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:103:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:17:in `block in autorun'
Excellent! I was able to figure it out.
Issues was in my actual UserMailer class (in /app/mailers/user_mailer.rb).
The default from: statement at the top did not match with my tests.
class UserMailer < ApplicationMailer
default from: "XXXXXXXX2#gmail.com" # <= This was the culprit
def activate_account(user)
#user = user
mail(:to => #user.email, :subject => "Please confirm to activate your account.")
end
end
I recently ran into this and my problem was that i hadn't provided a proper email address in the :to field
By mistake I had -
mail(to: #user)
Instead of
mail(to: #user.email)
Related
I have a module with a custom view helper. Actually it uses the modified code of link_to helper. Just append a query string to the generated link. I have existing tests which fails because the custom helper cannot be found. How can I include it so it is available for RSpec? I tried the following approaches:
1.Using include:
describe MyClass do
include MyHelper
2.Using configure
RSpec.configure do |config|
config.include MyHelper
end
3.Adding it in spec_helper.rb
config.include MyHelper
Here is the error:
1) Mailer#calendar_item_notification should send email for task
Failure/Error: expect {Mailer.calendar_item_notification('User', #user.id).deliver}.to change { ActionMailer::Base.deliveries.count }.by(1)
ActionView::Template::Error:
undefined method `<<' for #<Proc:0x00000013102d28>
# ./app/helpers/mailer_helper.rb:88:in `link_username'
# ./app/views/user_mailer/calendar_item_notification.html.erb:137:in `_app_views_user_mailer_calendar_item_notification_html_erb___206027131771876632_159436760'
# ./app/mailers/user_mailer.rb:136:in `calendar_item_notification'
# ./spec/mailers/user_mailer_spec.rb:19:in `block (4 levels) in <top (required)>'
# ./spec/mailers/user_mailer_spec.rb:19:in `block (3 levels) in <top (required)>'
# ./spec/support/misc.rb:13:in `block in suppress_output'
# ./spec/support/misc.rb:12:in `tap'
# ./spec/support/misc.rb:12:in `suppress_output'
# -e:1:in `<main>'
Thanks
I'm Trying to build a simple Blog using rails 4 and For authentication I'm using Devise.
The blog is simple and it will only have one registered user which is the admin. so the registration page should be available when there are no users in the database else it will redirect to the home page.
I'm trying to learn testing in the process. So I started testing the RegistrationContrroler However it doesn't seem to pass.
class RegistrationsController < Devise::RegistrationsController
before_action :one_user_registered?, only: [:new, :create]
protected
def one_user_registered?
if ((User.count == 1) & (user_signed_in?))
redirect_to root_path
elsif User.count == 1
redirect_to new_user_session_path
end
end
end
Specs:
RSpec.describe RegistrationsController, type: :controller do
context "Admin already registred" do
it "cannot create new user" do
#user_attr = FactoryGirl.attributes_for(:user)
# Create Admin
User.create!(#user_attr)
#request.env["devise.mapping"] = Devise.mappings[:user]
post :create, user: #user_attr
User.count should eq_to(1)
end
end
end
spec/factories.rb :
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "user#{n}#example.com" }
password "secure"
end
factory :post do
sequence(:id){ |n| n }
sequence(:title) { |n| "Post title #{n}" }
body "Post body"
sequence(:slug) { |n| "Post title #{n}" }
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "registrations" }
authenticated :user do
resources :posts
end
unauthenticated :user do
resources :posts, only: [:show, :index]
end
root 'posts#index'
end
Stacktrace :
F
Failures:
1) RegistrationsController Admin already registred cannot create new user
Failure/Error: User.count should eq_to(1)
expected: 1
got: #<RegistrationsController:0x00000003a413b8 #_action_has_layout=true, #_routes=nil, #_headers={"Conten...enticatable]>, #req touest_format=:html, #marked_for_same_origin_verification=false, #current_user=nil>
(compared using ==)
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-support-3.5.0/lib/rspec/support.rb:87:in `block in <module:Support>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-support-3.5.0/lib/rspec/support.rb:96:in `notify_failure'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/fail_with.rb:27:in `fail_with'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:38:in `handle_failure'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:50:in `block in handle_matcher'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:27:in `with_matcher'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:48:in `handle_matcher'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/memoized_helpers.rb:81:in `should'
# ./spec/controllers/registrations_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:252:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:252:in `block in run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:494:in `block in with_around_and_singleton_context_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:451:in `block in with_around_example_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:471:in `block in run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:611:in `block in run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-rails-3.5.1/lib/rspec/rails/example/controller_example_group.rb:191:in `block (2 levels) in <module:ControllerExampleGroup>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:382:in `execute_with'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:613:in `block (2 levels) in run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-rails-3.5.1/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:382:in `execute_with'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:613:in `block (2 levels) in run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:614:in `run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:471:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:451:in `with_around_example_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:494:in `with_around_and_singleton_context_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:249:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:627:in `block in run_examples'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:623:in `map'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:623:in `run_examples'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:589:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `block in run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `map'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `block (3 levels) in run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `map'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `block (2 levels) in run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/configuration.rb:1836:in `with_suite_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:112:in `block in run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/reporter.rb:77:in `report'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:111:in `run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:87:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:71:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:45:in `invoke'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/exe/rspec:4:in `<top (required)>'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `load'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `<main>'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Finished in 0.08188 seconds (files took 1.95 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/registrations_controller_spec.rb:7 # RegistrationsController Admin already registred cannot create new user
You're missing a dot!
User.count.should eq_to(1)
Adding a should method is the way rspec patches all objects to support the should comparisons.
Preferable would be...
expect(User.count).to eq(1)
The error showed that the should by itself was received by the RegistrationsController, not by the User.count... and of course the RegistrationsController doesn't equal 1.
This test gives no reason as to why it fails:
it "saves the new job in the database" do
expect{
post :create, booking: FactoryGirl.attributes_for(:booking)
}.to change(Booking,:count).by(1)
end
The test seems simple enough, and the functionality works in the site.
UPD For reference, here's the controller it's testing:
def create
#booking = Booking.new(booking_params)
#booking.save
respond_with(#booking)
end
And the factory:
FactoryGirl.define do
factory :booking do
employer
vehicle
job_type
location
start "2015-04-23 09:00"
ending "2015-04-23 15:00"
end
end
UPD Here's the rspec log, with -b:
BookingsController
as employer
GET #new
renders the :new template (PENDING: Not yet implemented)
assigns a new job to #job (PENDING: Not yet implemented)
GET #create
with invalid attributes
re-renders the :new template (PENDING: Not yet implemented)
does not save in the database (PENDING: Not yet implemented)
with valid attributes
redirects to the show page (PENDING: Not yet implemented)
in populate_times
saves the new job in the database (FAILED - 1)
GET #create
with invalid attributes
re-renders the :new template (PENDING: Not yet implemented)
does not save in the database (PENDING: Not yet implemented)
with valid attributes
redirects to the show page (PENDING: Not yet implemented)
saves the new job in the database (PENDING: Not yet implemented)
GET new
renders the new template
GET #show
renders the :show template (PENDING: Not yet implemented)
assigns the requested booking to #booking (PENDING: Not yet implemented)
as visitor
GET new
redirects to new_employer_path
Pending: (Failures listed here are expected and do not affect your suite's status)
1) BookingsController as employer GET #new renders the :new template
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:60
2) BookingsController as employer GET #new assigns a new job to #job
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:59
3) BookingsController as employer GET #create with invalid attributes re-renders the :new template
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:38
4) BookingsController as employer GET #create with invalid attributes does not save in the database
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:37
5) BookingsController as employer GET #create with valid attributes redirects to the show page
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:33
6) BookingsController as employer GET #create with invalid attributes re-renders the :new template
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:71
7) BookingsController as employer GET #create with invalid attributes does not save in the database
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:70
8) BookingsController as employer GET #create with valid attributes redirects to the show page
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:66
9) BookingsController as employer GET #create with valid attributes saves the new job in the database
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:65
10) BookingsController as employer GET #show renders the :show template
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:55
11) BookingsController as employer GET #show assigns the requested booking to #booking
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:43
Failures:
1) BookingsController as employer GET #create with valid attributes saves the new job in the database
Failure/Error: expect{
expected #count to have changed by 1, but was changed by 0
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/fail_with.rb:29:in `fail_with'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/handler.rb:40:in `handle_failure'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/handler.rb:50:in `block in handle_matcher'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/handler.rb:27:in `with_matcher'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/handler.rb:48:in `handle_matcher'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/expectation_target.rb:54:in `to'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/expectation_target.rb:87:in `to'
# ./spec/controllers/booking_controller_spec.rb:28:in `block (5 levels) in <top (required)>'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:177:in `instance_exec'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:177:in `block in run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:385:in `block in with_around_and_singleton_context_hooks'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:343:in `block in with_around_example_hooks'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:474:in `block in run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:614:in `block in run_around_example_hooks_for'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-rails-3.2.1/lib/rspec/rails/example/controller_example_group.rb:179:in `block (2 levels) in <module:ControllerExampleGroup>'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:387:in `execute_with'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:616:in `block (2 levels) in run_around_example_hooks_for'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-rails-3.2.1/lib/rspec/rails/adapters.rb:113:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:387:in `execute_with'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:616:in `block (2 levels) in run_around_example_hooks_for'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:617:in `run_around_example_hooks_for'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:474:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:343:in `with_around_example_hooks'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:385:in `with_around_and_singleton_context_hooks'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:174:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:549:in `block in run_examples'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:545:in `map'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:545:in `run_examples'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:512:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `block in run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `map'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `block in run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `map'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `block in run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `map'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:in `block (3 levels) in run_specs'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:in `map'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:in `block (2 levels) in run_specs'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1526:in `with_suite_hooks'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:109:in `block in run_specs'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/reporter.rb:62:in `report'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:108:in `run_specs'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:86:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/bin/rspec:23:in `load'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/bin/rspec:23:in `<main>'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `<main>'
I can't add comments cause of my reputation ... but maybe u shoud try to run rspec with -b option to see full backtraces. And detect the error
I have two methods in my Designer class (in my rails app):
def add_specialty(specialty)
specialty_list.add(specialty)
save
end
def add_qualification(qualification)
qualification_list.add(qualification)
save
end
Here are specs I have for them that are passing:
context 'adding specialties' do
it "can add a new specialty" do
expect { designer.add_specialty("interior design") }.to change {designer.specialty_list.count}.by(1)
expect(designer.specialty_list).to include("interior design")
end
end
context 'adding qualifications' do
it "can add a new qualification" do
expect { designer.add_qualification("architect") }.to change {designer.qualification_list.count}.by(1)
expect(designer.qualification_list).to include("architect")
end
end
Now I want to refactor to this implementation:
["specialty", "qualification"].each do |attr|
define_method("add_#{attr}") do |arg|
"#{attr}_list".add(arg)
save
end
end
This fails. I get failures:
1) Designer adding qualifications can add a new qualification
Failure/Error: expect { designer.add_qualification("architect") }.to change {designer.qualification_list.count}.by(1)
NoMethodError:
undefined method `add' for "qualification_list":String
# ./app/models/designer.rb:93:in `block (2 levels) in <class:Designer>'
# ./spec/models/designer_spec.rb:79:in `block (4 levels) in <top (required)>'
# ./spec/models/designer_spec.rb:79:in `block (3 levels) in <top (required)>'
# -e:1:in `<main>'
2) Designer adding specialties can add a new specialty
Failure/Error: expect { designer.add_specialty("interior design") }.to change {designer.specialty_list.count}.by(1)
NoMethodError:
undefined method `add' for "specialty_list":String
# ./app/models/designer.rb:93:in `block (2 levels) in <class:Designer>'
# ./spec/models/designer_spec.rb:72:in `block (4 levels) in <top (required)>'
# ./spec/models/designer_spec.rb:72:in `block (3 levels) in <top (required)>'
# -e:1:in `<main>'
What am I doing wrong in my define_method implementation?
"#{attr}_list" by itself is just the string "specialty_list" or "qualification_list", and strings don't have an add method. I think you want to the send the specialty_list method e.g.
%w{ specialty qualification }.each do |attr|
define_method("add_#{attr}") do |arg|
send("#{attr}_list").add(arg)
save
end
end
Ok: I got it working like this:
["specialty", "qualification"].each do |attr|
define_method("add_#{attr}") do |arg|
instance_eval("#{attr}_list").send(:add, arg)
save
end
end
Not sure why this worked though or if its the right way to do it. Would anyone care to contribute towards a better understanding?
This is what my spec file looks like: spec/api/v1/projects_spec.rb
require "spec_helper"
describe "/api/v1/projects", :type => :api do
context "projects viewable by this user" do
it "JSON" do
end
end
end
It contained a lot more but I deleted a lot of lines in an unsuccessful attempt to find the error which goes like this:
Failure/Error: Unable to find matching line from backtrace
NoMethodError: undefined method `env' for nil:NilClass
This piece of code is from the "Rails 3 in Action" by Ryan Bigg. The only other file it includes is: spec/support/api/helper.rb
module ApiHelper
include Rack::Test::Methods
def app
Rails.application
end
end
RSpec.configure do |c|
c.include ApiHelper, :type => :api
end
I've been trying to google the error for the past 1 hr and the closest thing that I found was this and as a result I deleted the code which used Devise::Test_Helpers. Sadly, its still not working.
Any help would be appreciated. Thank you so much.
Stack Trace
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/devise-2.0.4/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-rails-2.8.1/lib/rspec/rails/adapters.rb:15:in `block (2 levels) in setup'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:35:in `instance_eval'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:35:in `run_in'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `block in run_all'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `each'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `run_all'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:368:in `run_hook'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `block in run_before_each_hooks'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `each'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `run_before_each_hooks'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:217:in `run_before_each'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:79:in `block in run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:173:in `with_around_hooks'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:77:in `run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:355:in `block in run_examples'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:351:in `map'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:351:in `run_examples'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:337:in `run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `block in run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `map'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `map'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `block in run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/reporter.rb:34:in `report'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:25:in `run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun'
You're still including Devise::TestHelpers somewhere. Check and see what "spec_helper" is loading.
Instead of just including the Devise Test Helper, you can specify it's type, so that it's not included in routing spec:
config.include Devise::TestHelpers, :type => :helper
config.include Devise::TestHelpers, :type => :controller