Rspec tests in Rails suddenly start to fail - ruby-on-rails

I was changing some parts of code, and my tests started to give errors and warnings.
Then I removed those tests, because I couldn't fix them.
But then tests in my other file totally went awry.
The following is my factories.rb file:
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:surname) { |n| "Persona #{n}" }
sequence(:email) { |n| "person_persona_#{n}#example.com.eu" }
password "foobar"
password_confirmation "foobar"
# factory :admin do
# admin true
# end
factory :admin do
role "admin"
end
factory :editor do
role "editor"
end
factory :author do
role "author"
end
end
factory :course do
sequence(:title) { |n| "Title #{n}" }
sequence(:objectives) { |n| "Objectives #{n}" }
user_id 1
subject_id 1
student_level_id 1
end
factory :subject do
title "French for Adults"
end
factory :student_level do
title "Advanced"
end
end
And this is my actual test file, models/course_spec.rb:
require 'spec_helper'
describe Course do
let (:user) { FactoryGirl.create(:user) }
let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
let (:student_level) { FactoryGirl.create(:student_level) }
before do
# #course = Course.new(user_id: user.id, subject_id: subject_.id, student_level_id: student_level.id,
# title: "French for Us", objectives: "Lorem ipsum")
#course = user.courses.build(title: "French", objectives: "lorem")
#course.subject = subject_
#course.student_level = student_level
end
subject { #course }
it { should respond_to(:user_id) }
it { should respond_to(:subject_id) }
it { should respond_to(:student_level_id) }
it { should respond_to(:title) }
it { should respond_to(:objectives) }
it { should respond_to(:user) }
its(:user) { should == user }
it { should be_valid }
describe "when user_id is not present" do
before { #course.user_id = nil }
it { should_not be_valid }
end
describe "when subject_id is not present" do
before { #course.subject_id = nil }
it { should_not be_valid }
end
describe "when student_level_id is not present" do
before { #course.student_level_id = nil }
it { should_not be_valid }
end
describe "when title is not present" do
before { #course.title = "" }
it { should_not be_valid }
end
describe "when title is too long" do
before { #course.title = "a" * 251 }
it { should_not be_valid }
end
describe "when title is already taken" do
before do
course_with_same_title = #course.dup
course_with_same_title.title = #course.title.upcase
course_with_same_title.save
end
it { should_not be_valid }
end
describe "when objectives are not present" do
before { #course.objectives = "" }
it { should_not be_valid }
end
describe "accessible attributes" do
it "should not allow access user_id" do
expect do
Course.new(user_id: user.id)
end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
# it "should not allow access subject_id" do
# expect do
# Course.new(subject_id: subject_.id)
# end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
# end
# it "should not allow access student_level_id" do
# expect do
# Course.new(student_level_id: student_level.id)
# end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
# end
end
end
And these are test outputs:
:~/ror/oy$ bundle exec rspec
...............................................................FFFFFFFFFFFFFFFF.................................................
Failures:
1) Course
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
2) Course
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
3) Course
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
4) Course
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
5) Course
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
6) Course
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
7) Course
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
8) Course when user_id is not present
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
9) Course user
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
10) Course when title is already taken
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
11) Course when objectives are not present
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
12) Course accessible attributes should not allow access user_id
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
13) Course when title is too long
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
14) Course when subject_id is not present
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
15) Course when student_level_id is not present
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
16) Course when title is not present
Failure/Error: let (:subject_) { FactoryGirl.create(:subject) } # "_" so that to differenciate from subject test directive
ActiveRecord::RecordInvalid:
translation missing: uzlt.activerecord.errors.messages.record_invalid
# ./spec/models/course_spec.rb:19:in `block (2 levels) in <top (required)>'
# ./spec/models/course_spec.rb:27:in `block (2 levels) in <top (required)>'
Finished in 7.82 seconds
128 examples, 16 failures
Failed examples:
rspec ./spec/models/course_spec.rb:36 # Course
rspec ./spec/models/course_spec.rb:37 # Course
rspec ./spec/models/course_spec.rb:34 # Course
rspec ./spec/models/course_spec.rb:35 # Course
rspec ./spec/models/course_spec.rb:33 # Course
rspec ./spec/models/course_spec.rb:41 # Course
rspec ./spec/models/course_spec.rb:38 # Course
rspec ./spec/models/course_spec.rb:45 # Course when user_id is not present
rspec ./spec/models/course_spec.rb:39 # Course user
rspec ./spec/models/course_spec.rb:76 # Course when title is already taken
rspec ./spec/models/course_spec.rb:82 # Course when objectives are not present
rspec ./spec/models/course_spec.rb:86 # Course accessible attributes should not allow access user_id
rspec ./spec/models/course_spec.rb:65 # Course when title is too long
rspec ./spec/models/course_spec.rb:50 # Course when subject_id is not present
rspec ./spec/models/course_spec.rb:55 # Course when student_level_id is not present
rspec ./spec/models/course_spec.rb:60 # Course when title is not present
Randomized with seed 44955
I suppose there is problem with factories.rb file. But I checked it with the same file on other branch, nothing changed actually.
What could be wrong here?

I ran
$ rake db:test:prepare
per this thread: Suddenly ALL RSpec tests failing?
And everything worked fine.

I suggest you check the following things
schema, migration: make sure your object has the correct attributes
model validation: this is highly the reason that cause your test fail. Last time I had this invalid_record error is because of a validation fault

Related

Rspec 'trait not registered error when using Faker

I am trying to use Faker in FactoryBot in my Rails API tests.
The issue I am having that I keep getting this error:
KeyError:
Trait not registered: "first_name"
So far as I can tell I have configured everything right, based on Faker'd docs at least. So far as I can tell I have two actual traits, valid_user and invalid_user that I set like so...
let(:valid_attributes) { FactoryBot.attributes_for :api_v1_user, :valid_user }
let(:invalid_attributes) { FactoryBot.attributes_for :api_v1_user, :invalid_user }
Which are linked to the :api_v1_user factory.
What have I missed or done wrong?
Full Error:
8) /api/v1/users PATCH /update with invalid parameters renders a JSON response with errors for the api/v1_user
Failure/Error: let(:invalid_attributes) { FactoryBot.attributes_for(:api_v1_user, :invalid_user) }
KeyError:
Trait not registered: "first_name"
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/activesupport-6.0.2.2/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/activesupport-6.0.2.2/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/registry.rb:23:in `find'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/decorator.rb:10:in `method_missing'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/internal.rb:49:in `trait_by_name'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:114:in `trait_by_name'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:106:in `block in base_traits'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:106:in `map'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:106:in `base_traits'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:51:in `block in compile'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:50:in `compile'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:130:in `aggregate_from_traits_and_self'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:34:in `to_create'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/trait.rb:17:in `to_create'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:135:in `map'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:135:in `aggregate_from_traits_and_self'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:34:in `to_create'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition_hierarchy.rb:16:in `build_from_definition'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/factory.rb:125:in `build_hierarchy'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/factory.rb:88:in `compile'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/factory.rb:32:in `run'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/factory_runner.rb:29:in `block in run'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/activesupport-6.0.2.2/lib/active_support/notifications.rb:182:in `instrument'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/factory_runner.rb:28:in `run'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/requests/api/v1/users_spec.rb:6:in `block (2 levels) in <top (required)>'
# ./spec/requests/api/v1/users_spec.rb:92:in `block (4 levels) in <top (required)>'
# ./spec/rails_helper.rb:19:in `block (3 levels) in <top (required)>'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/database_cleaner-1.8.4/lib/database_cleaner/generic/base.rb:16:in `cleaning'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:87:in `block (2 levels) in cleaning'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:88:in `cleaning'
# ./spec/rails_helper.rb:18:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# KeyError:
# key not found: "first_name"
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/activesupport-6.0.2.2/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
spec/factories/api/v1/users.rb
require 'faker'
FactoryBot.define do
factory :api_v1_user, class: 'Api::V1::User' do
trait :valid_user do
first_name { Faker::Name.first_name }
second_name { Faker::Name.second_name }
username { Faker::Internet.username }
email { Faker::Internet.safe_email }
password { Faker::Internet.password(min_length: 8) }
end
trait :invalid_user do
first_name nil
end
end
end
rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
RSpec.configure do |config|
config.use_transactional_fixtures = false
DatabaseCleaner.strategy = :truncation
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
config.before(:all) do
DatabaseCleaner.start
end
config.after(:all) do
DatabaseCleaner.clean
end
end
users_spec.rb
Truncated
RSpec.describe "/api/v1/users", type: :request do
let(:valid_attributes) { FactoryBot.attributes_for :api_v1_user, :valid_user }
let(:invalid_attributes) { FactoryBot.attributes_for :api_v1_user, :invalid_user }
let(:valid_headers) {
{}
}
describe "PATCH /update" do
context "with invalid parameters" do
it "renders a JSON response with errors for the api/v1_user" do
user = Api::V1::User.create! valid_attributes
patch api_v1_user_url(user),
params: { api_v1_user: invalid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq("application/json")
end
end
end
end
Try wrapping the nil in brackets. FactoryBot eliminated support for static attributes in version 5, so if you're using a newer version, that'll be your problem. Brackets are now required.
trait :invalid_user do
first_name { nil }
end

RSpec respond_to does not work

This is my code snippet to test the polymorphic relationship.
I wrote test as below.
require 'rails_helper'
RSpec.describe Proration, type: :model do
let(:invoice_item) { FactoryGirl.create(:proration_invoice_item) }
subject { :invoice_item }
it { should be_valid }
it { should respond_to(:total_amount) }
it { should respond_to(:invoice) }
it { should respond_to(:itemable) }
end
But this errors out
Failures:
1) Proration should be valid
Failure/Error: it { should be_valid }
NoMethodError:
undefined method `valid?' for :invoice_item:Symbol
# ./spec/models/proration_spec.rb:7:in `block (2 levels) in <top (required)>'
2) Proration Proration should respond to #total_amount
Failure/Error: end
expected :invoice_item to respond to :total_amount
# ./spec/models/proration_spec.rb:16:in `block (3 levels) in <top (required)>'
3) Proration Proration should respond to #invoice
Failure/Error: end
expected :invoice_item to respond to :invoice
# ./spec/models/proration_spec.rb:17:in `block (3 levels) in <top (required)>'
4) Proration Proration should respond to #itemable
Failure/Error: Unable to find matching line in /Users/toshikiinami/Desktop/billing/spec/models/proration_spec.rb
expected :invoice_item to respond to :itemable
# ./spec/models/proration_spec.rb:18:in `block (3 levels) in <top (required)>'
Any ideas to fix this?
(respond_to for model doesn't work.)
You defined an invoice_item using let, but as a subject you're using symbol. What you want instead is that object invoice_item, so
subject{ :invoice_item }
should be changed to
subject{ invoice_item }

Why is my define_method not working?

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?

rspec factory girl not creating user

I'm using rails 3.2 with devise and rspec with factory girl. I'm trying to log a user in but it seems there is a problem creating the user in the first place.
user factory:
factory :user do
first_name "Test"
last_name "User"
sequence(:email) { |n| "foo#{n}#test.com" }
password "secretpassword"
confirmed_at Time.now
end
my spec
feature "BackOffice" do
subject { page }
context "as user" do
let(:user) { create(:user) }
describe "should not have access to backoffice" do
before do
visit "http://domain_name/fr/users/sign_in"
within ".login-wrapper" do
fill_in "user_email", with: user.email
fill_in "user_password", with: user.password
click_button "S'identifier"
end
end
it { should have_content "Bienvenue"}
end
end
Error
Failure/Error: let(:user) { create(:user) }
ActionView::Template::Error:
No route matches {:action=>"edit", :controller=>"users/registrations"}
# ./app/views/user_mailer/welcome.html.erb:3:in `_app_views_user_mailer_welcome_html_erb__212088972377821759_70301344598820'
# ./app/mailers/user_mailer.rb:31:in `welcome'
# ./app/models/user.rb:125:in `send_welcome_email'
# ./spec/requests/backoffice_spec.rb:15:in `block (3 levels) in <top (required)>'
# ./spec/requests/backoffice_spec.rb:20:in `block (5 levels) in <top (required)>'
# ./spec/requests/backoffice_spec.rb:19:in `block (4 levels) in <top (required)>'
# ./spec/support/database_cleaner.rb:17:in `block (2 levels) in <top (required)>'
why would it be trying to go to the edit action when it should be creating the resource?
There was a problem with a link in a welcome email I added the following to fix it:
config.action_mailer.default_url_options = { protocol: "http", host: "localhost", port: 3000, locale: I18n.locale }

How to access rails request object in rspec integration test?

In Rails 3.2.13, subdomain is provided by default. As such, I want to test my application such that when a merchant signs up, he goes by default to root_url with subdomain 'merchant' i.e. https://merchant.lvh.me:3000. However, I am having trouble testing the same in RSpec. My test looks like this:
describe "Sign in" do
before { visit signup_path }
let(:submit) { "Sign up" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Email", with: "user#gmail.com"
fill_in "Password", with: "securepassword"
end
describe "as a merchant" do
before { choose("Merchant") }
it "should create a merchant user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after creating the merchant user" do
before do
click_button submit
request = ActionController::TestRequest.new(:host => "lvh.me:3000")
end
let(:merchant) { User.find_by_email('user#gmail.com') }
expect (request.subdomain).to eq('merchant')
it { should have_selector 'div.alert.alert-success', text: "Welcome to App!" }
it { should have_link "Sign out", href: signout_path }
it { should_not have_link "Sign in", href: signin_path }
it { should have_content merchant.email }
it { should have_selector "title", text: full_title(merchant.email) }
end
end
describe "as a user" do
before do
choose("User")
request = ActionController::TestRequest.new(:host => "lvh.me:3000")
end
it "should create a normal user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after creating the normal user" do
before { click_button submit }
let(:user) { User.find_by_email('user#gmail.com') }
expect (request.subdomain).to eq('user')
it { should have_selector 'div.alert.alert-success', text: "Welcome to App!" }
it { should have_link "Sign out", href: signout_path }
it { should_not have_link "Sign in", href: signin_path }
it { should have_content user.email }
it { should have_selector "title", text: full_title(user.email) }
end
end
end
end
Error is:
19:58:54 - INFO - Guard::RSpec is running, with RSpec 2!
19:58:54 - INFO - Running all specs
Running tests with args ["--drb", "-f", "progress", "-r", "/usr/local/lib/ruby/gems/1.9.1/gems/guard-rspec-1.2.1/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]...
Exception encountered: #<NameError: undefined local variable or method `request' for #<Class:0x007fe0d8058848>>
backtrace:
/home/app/spec/requests/user_pages_spec.rb:46:in `block (5 levels) in <top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
/home/app/spec/requests/user_pages_spec.rb:39:in `block (4 levels) in <top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
/home/app/spec/requests/user_pages_spec.rb:32:in `block (3 levels) in <top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
/home/app/spec/requests/user_pages_spec.rb:26:in `block (2 levels) in <top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
/home/app/spec/requests/user_pages_spec.rb:16:in `block in <top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/dsl.rb:18:in `describe'
/home/app/spec/requests/user_pages_spec.rb:3:in `<top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `block in load'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/test_framework/rspec.rb:11:in `run_tests'
/usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:13:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:21:in `block in initialize'
/usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:18:in `fork'
/usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:18:in `initialize'
/usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `new'
/usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/server.rb:48:in `run'
/usr/local/lib/ruby/1.9.1/drb/drb.rb:1548:in `perform_without_block'
/usr/local/lib/ruby/1.9.1/drb/drb.rb:1508:in `perform'
/usr/local/lib/ruby/1.9.1/drb/drb.rb:1586:in `block (2 levels) in main_loop'
/usr/local/lib/ruby/1.9.1/drb/drb.rb:1582:in `loop'
/usr/local/lib/ruby/1.9.1/drb/drb.rb:1582:in `block in main_loop'
Done.
I tried to print request object to see if anything is there, but its blank. How to access request object in RSpec integration test or is integration test the right place to test for this kind of behavior? Please suggest.
Edit: More Information My data model consists of only single user table where all by default are normal users. However, some are merchants also. Now, when someone signs-in as a user (distinguished using radio button in sign-in form), he should be redirected to user.lvh.me and if he signs-in as a merchant, he should be redirected to merchant.lvh.me. This is what I am trying to test in my integration tests.
You can mock a request using
ActionController::TestRequest.new()
for example
request = ActionController::TestRequest.new(:host => test_domain)
I'm not really clear what behaviour you are trying to test here.
It seems that you have a sign up form where merchants can sign up. Once they have signed up they should get their own subdomain ( merchant1.lvh.me, merchant2.lvh.me). Is that right?
If that is right, then it looks like you are trying to test that the sub-domain for the sign up request should be set. I don't think thats how it works - the client is in control of the domain that the request is made to not the server. I suspect what you want to do is perform a redirect after successful signup to the appropriate subdomian. Thus you want to test that the response is redirecting to the subdomain.

Resources