I was working on the part 9.3.3 about pagination from the ruby on rails tutorial of Michael Hartl, an Im stuck: running the rspec tests i got an error message that there is something wrong with the gem FactoryGirl. Do you suppose what could be a problem?
/Users/smi/.rvm/gems/ruby-2.0.0-p594/gems/factory_girl-4.5.0/lib/factory_girl/definition_proxy.rb:43:in `add_attribute': Both value and block given (FactoryGirl::AttributeDefinitionError)
from /Users/smi/.rvm/gems/ruby-2.0.0-p594/gems/factory_girl-4.5.0/lib/factory_girl/definition_proxy.rb:102:in `method_missing'
from /Users/smi/projects/sample_app/spec/factories.rb:3:in `block (2 levels) in <top (required)>'
from /Users/smi/.rvm/gems/ruby-2.0.0-p594/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:18:in `instance_eval'
from /Users/smi/.rvm/gems/ruby-2.0.0-p594/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:18:in `factory'
from /Users/smi/projects/sample_app/spec/factories.rb:2:in `block in <top (required)>'
from /Users/smi/.rvm/gems/ruby-2.0.0-p594/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
from /Users/smi/.rvm/gems/ruby-2.0.0-p594/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:49:in `run'
from /Users/smi/.rvm/gems/ruby-2.0.0-p594/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:7:in `define'
from /Users/smi/projects/sample_app/spec/factories.rb:1:in `<top (required)>'
from /Users/smi/.rvm/ge......................
users_pages_spec.rb:
describe "pagination" do
before(:all) { 30.times { FactoryGirl.create(:user) } }
after(:all) { User.delete_all }
it { should have_selector('div.pagination') }
it "should list each user" do
User.paginate(page: 1).each do |user|
expect(page).to have_selector('li', text: user.name)
end
end
end
factories.rb:
FactoryGirl.define do
factory :user do
sequense(:name) { |n| "Person #{n}" }
sequense(:email) { |n| "person_#{n}#example.com" }
password "foobar"
password_confirmation "foobar"
end
end
I have found an answer! the method "sequence" in factories.rb was written with a mistake. Now all tests are green.
Related
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 }
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
I'm following Michael Hartl's great book on Ruby on Rails(rails 3.2 version). I'm having some problems in section 9.3.3. Pagination.
After I modified my factory at spec/factories.rb using sequence like this(note that I have the previous version commented out):
FactoryGirl.define do
#factory :user do
# name "Michael Hartl"
# email "michael#example.com"
# password "foobar"
# password_confirmation "foobar"
#end
factory :user do
sequence(:user){ |n| "Person #{n}" }
sequence(:email){ |n| "person_#{n}#example.com"}
password "foobar"
password_confirmation "foobar"
end
end
I cannot use this anymore in my tests:
let(:user){ FactoryGirl.create(:user) }
For instance the test with code:
require 'spec_helper'
describe "User pages" do
subject { page }
describe "index" do
let(:user){ FactoryGirl.create(:user) }
before(:all){ 30.times {FactoryGirl.create(:user) }}
after(:all) {User.delete_all}
before(:each) do
valid_signin user
visit users_path
end
it { should have_selector('title', text: 'All users') }
it { should have_selector('h1', text: 'All users') }
.
.
.
end
Returns errors such as:
Failure/Error: before(:all){ 30.times {FactoryGirl.create(:user) }}
NoMethodError:
undefined method `user=' for #<User:0x00000002760140>
# ./spec/requests/user_pages_spec.rb:16:in `block (4 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:16:in `times'
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
It somehow makes sense that I can't use this syntax (let(:user){...}) anymore, since I'm now creating sequences of elements, but I can't seem to find a fix for this.
Any ideas? thanks!
I think you mean sequence(:name) instead of sequence(:user) in your factory. It's looking for a method user= in your User model instead of the name= method.
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.
I'm having trouble getting the test to pass in Michael Hartl's Ruby on Rails Tutorial Chapter 7.1.3.
Here is the error message:
Failures:
1) User pages profile page
Failure/Error: let(:user) { FactoryGirl.create(:user) }
ArgumentError:
Factory not registered: user
# ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
2) User pages profile page
Failure/Error: let(:user) { FactoryGirl.create(:user) }
ArgumentError:
Factory not registered: user
# ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 2.72 seconds
37 examples, 2 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:11 # User pages profile page
rspec ./spec/requests/user_pages_spec.rb:12 # User pages profile page
This is my user_pages_spec.rb:
require 'spec_helper'
describe "User pages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: 'Sign up') }
end
end
This is my factories.rb:
FactoryGirl.define do
factory :user do
name "Michael Hartl"
email "mhartl#example.com"
password "foobar"
password_confirmation "foobar"
end
end
And this is the Gemfile:
group :test do
gem 'capybara', '1.1.2'
gem 'cucumber-rails', '1.2.1', :require => false
gem 'database_cleaner', '0.7.0'
gem 'factory_girl_rails', '1.4.0'
end
I've tried rebooting the server, and running bundle exec rake db:reset, db:test:prepare, db:test:load, db:migrate - nothing seems to work. Any ideas?
If you are following Michael Hartl's guide, it is possible that you may be using spork and guard too. Updates in factories are not noted by spork, unless spork is restarted.
If you are using them, try restarting spork and guard