Create Multiple Factories for User - ruby-on-rails

Earlier I was having single factory for user and it worked fine.
But now, I want two sets of factories for user's data , one with encrypted details and other one with plain text user details.
This is the code I am using:
FactoryBot.define do
factory :user do
trait :encryption do
email AESCrypt.encrypt(Faker::Internet.email, ENV["AES_KEY"])
password AESCrypt.encrypt("password", ENV["AES_KEY"])
password_confirmation AESCrypt.encrypt("password",ENV["AES_KEY"])
username Faker::Name.name
end
trait :unencrypted_user_details do
email Faker::Internet.email
password "password"
password_confirmation "password"
username Faker::Name.name
end
end
end
And using the same as in spec file:
user = FactoryBot.create(:user,:unencrypted_user_details)
But I am getting the following error while running the spec:
NoMethodError:
undefined method `name=' for #<User:0x00000006d512f8>
The User model does not have a field "name" instead "username" is there.
Error Stacktrace:
F
Failures:
1) Api::V2::UserApp::UsersController Generate Pin API generates a new pin for the user
Failure/Error: user = FactoryBot.create(:unencrypted_user_details)
NoMethodError:
undefined method `name=' for #<User:0x0000000664cbb0>
Did you mean? name
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/activemodel-4.2.7.1/lib/active_model/attribute_methods.rb:433:in `method_missing'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:16:in `public_send'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:16:in `block (2 levels) in object'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:15:in `each'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:15:in `block in object'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:14:in `tap'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:14:in `object'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/evaluation.rb:13:in `object'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/strategy/create.rb:9:in `result'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/factory.rb:43:in `run'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:29:in `block in run'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/notifications.rb:166:in `instrument'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:28:in `run'
# /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/controllers/user_app/users_controller.rb:43:in `block (3 levels) in <top (required)>'
Finished in 0.37745 seconds (files took 4.26 seconds to load)
3 examples, 1 failure
user_spec.rb
describe "Generate Pin API" do
it "generates a new pin for the user" do
user = FactoryBot.create(:unencrypted_user_details) #line 43
client = user.client
request.env["HTTP_AUTHORIZATION"] = user.auth_token
get :generate_pin
response_json = JSON.parse(response.body)
expect(response_json["response_code"]).to eq(200)
end
end
Controller Code
def generate_pin
phone = ValidatePhone.find_by(phone_no: #client.phone_no)
phone.present? ? phone.update_attributes(is_verified: true) : ValidatePhone.create(phone_no: #client.phone_no)
sms_text = "Hello"
send_sms(#client.phone_no,sms_text)
render :json => {
:response_code => 200,
:response_message => "Welcome Onboard."
}
end
Is this the correct way to create multiple factories and why am I getting this error undefined method name?

This is not the way to create variable factories. You must use brackets for fields on which value is the result fo a call:
FactoryBot.define do
factory :user do
trait :encryption do
email { AESCrypt.encrypt(Faker::Internet.email, ENV["AES_KEY"]) }
password { AESCrypt.encrypt("password", ENV["AES_KEY"]) }
password_confirmation { AESCrypt.encrypt("password",ENV["AES_KEY"]) }
username { Faker::Name.name }
end
trait :unencrypted_user_details do
email { Faker::Internet.email }
password "password"
password_confirmation "password"
username { Faker::Name.name }
end
end
end
Take a look at the guides:
https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#traits

Related

Rails 4, Rspec and Factory Girl: URI::InvalidURIError (Multitenancy)

Im following a tutorial on Rails 4 and Multitenancy, but am getting an error when trying to test if a user can sign in as an owner and redirect to a created subdomain.
This is the test error:
Failures:
1) User sign in signs in as an account owner successfully
Failure/Error: visit root_url
URI::InvalidURIError:
bad URI(is not URI?): http://#{account.subdomain}.example.com
# /Users/developer/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/rack_test/browser.rb:71:in `reset_host!'
# /Users/developer/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/rack_test/browser.rb:21:in `visit'
# /Users/developer/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/rack_test/driver.rb:43:in `visit'
# /Users/developer/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/session.rb:240:in `visit'
# /Users/developer/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
# ./spec/features/users/sign_in_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.56981 seconds (files took 1.26 seconds to load)
11 examples, 1 failure, 6 pending
This is the test:
require "rails_helper"
feature "User sign in" do
extend SubdomainHelpers
let!(:account) { FactoryGirl.create(:account) }
let(:sign_in_url) {"http://#{account.subdomain}.example.com/sign_in"}
let(:root_url) {"http://#{account.subdomain}.example.com/"}
within_account_subdomain do
scenario "signs in as an account owner successfully" do
visit root_url
expect(page.current_url).to eq(sign_in_url)
fill_in "Email", :with => account.owner.email
fill_in "Password", :with => "password"
click_button "Sign in"
expect(page).to have_content("You are now signed in.")
expect(page.current_url).to eq(root_url)
end
end
end
Here are the factories:
Account:
FactoryGirl.define do
factory :account, :class => Subscribe::Account do
sequence(:name) { |n| "Test Account ##{n}" }
sequence(:subdomain) { |n| "test#{n}" }
association :owner, :factory => :user
end
end
User:
FactoryGirl.define do
factory :user, :class => Subscribe::User do
sequence(:email) { |n| "test#{n}#example.com" }
password "password"
password_confirmation "password"
end
end
I am really not familiar with BDD, please let me know if you need me to post anything further.
So I solved this:
The problem was in my SubdomainHelpers file
module SubdomainHelpers
def within_account_subdomain
### This Line Is the original line
let(:subdomain_url) { 'http://#{account.subdomain}.example.com' }
### Changed it to this
let(:subdomain_url) { "http://#{account.subdomain}.example.com" }
before { Capybara.default_host = subdomain_url }
after { Capybara.default_host = 'http://www.example.com' }
yield
end
end
For some reason using single quotes was keeping account.subdomain as a string; as soon as I changed to double quotes the test passed!
Thanks.

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 }

RSPEC failed with email formats

I have been browsing the Rails tutorial with putting together the user account and running tests on it for the project I am working on.
Failures:
1) when email format is invalid should be invalid
Failure/Error: #user.email = invalid_address
NoMethodError:
undefined method `email=' for nil:NilClass
# ./spec/models/user_spec.rb:71:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:70:in `each'
# ./spec/models/user_spec.rb:70:in `block (2 levels) in <top (required)>'
2) when email format is valid should be valid
Failure/Error: #user.email = valid_address
NoMethodError:
undefined method `email=' for nil:NilClass
# ./spec/models/user_spec.rb:81:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:80:in `each'
# ./spec/models/user_spec.rb:80:in `block (2 levels) in <top (required)>'
Finished in 0.527 seconds
9 examples, 2 failures
Failed examples:
rspec ./spec/models/user_spec.rb:67 # when email format is invalid should be invalid
rspec ./spec/models/user_spec.rb:78 # when email format is valid should be valid
I have no clue what the problem is. I see what it says, but I even C&P the code from the tutorial to double check what I did to ensure everything was type properly.
Here's the user_spec file.
https://gist.github.com/pwz2k/4770845
Here's the user.rb file.
https://gist.github.com/pwz2k/4770854
The fails did not appear until I added the email validation.
Make sure that you're creating #user before you start testing it.. do you have these lines in your User test? (rails tutorial listing 6.16)
before do
#user = User.new(name: "Example User", email: "user#example.com")
end
The error is very clear!. You're setting email to nil class. Which means you're supposed to set the email to a user which we say
#user.email = "something"
The error is complaining there is no user, and there won't be any email for user.
To make is work here is an example code which will help you to fix this issue.
describe "validations" do
before(:each) do
#user = User.new(name: "gates", email: "somename#gmail.com")
end
it "should be invalid" do
invalid_emails = %w{gs#gmail p.com name.gmail.com foo.ymail}
invalid_emails.each do |invalid_email|
#user.email = invalid_email
expect(#user.email).to_not be_valid
end
end
it "should be valid" do
#user.name = "somename"
valid_emails = %w{user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp
a+b#baz.cn}
valid_emails.each do |valid_email|
#user.email = valid_email
expect(#user.email).to be_valid
end
end
end
Please be make sure you've created a fixure for user in your fixures folder.
Hope this helps!

how to get methods via has_one and belongs_to assocation?

I have few weeks experience or close to a month learning on Ruby on Rails. I hope to understand why I can't get methods via associated objects.
I am attempting to get associated objects attached to user instead of stane alone Profile.new e.g. profile with its methods as seen just below.
user.profile.create
user.profile.create!
user.profile.build
instead i get RSpec error messages. NoMethodError:undefined method `build' for nil:NilClass
Thanks kindly in advance
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ActiveRecord::Base
attr_accessible :email
has_one :profile
before_save { |user| user.email = email.downcase }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
end
.
# == Schema Information
#
# Table name: profiles
#
# id :integer not null, primary key
# given_name :string(255)
# surname :string(255)
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Profile < ActiveRecord::Base
attr_accessible :given_name, :surname #, :user_id
belongs_to :user
validates :user_id, presence: true
end
.
smileymike#ubuntu:~/rails_projects/bffmApp$ bundle exec guard
Guard uses Libnotify to send notifications.
Guard is now watching at '/home/smileymike/rails_projects/bffmApp'
Starting Spork for RSpec
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
Spork server for RSpec successfully started
Guard::RSpec is running, with RSpec 2!
Running all specs
Running tests with args ["--drb", "-f", "progress", "-r", "/home/smileymike/.rvm/gems/ruby-1.9.3-p194/gems/guard-rspec-0.7.2/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]...
......FFFFFFFF...............
Failures:
1) Profile
Failure/Error: before { #profile = user.profile.build(given_name: "Michael", surname: "Colin") }
NoMethodError:
undefined method `build' for nil:NilClass
# ./spec/models/profile_spec.rb:23:in `block (2 levels) in <top (required)>'
2) Profile
Failure/Error: before { #profile = user.profile.build(given_name: "Michael", surname: "Colin") }
NoMethodError:
undefined method `build' for nil:NilClass
# ./spec/models/profile_spec.rb:23:in `block (2 levels) in <top (required)>'
3) Profile
Failure/Error: before { #profile = user.profile.build(given_name: "Michael", surname: "Colin") }
NoMethodError:
undefined method `build' for nil:NilClass
# ./spec/models/profile_spec.rb:23:in `block (2 levels) in <top (required)>'
4) Profile
Failure/Error: before { #profile = user.profile.build(given_name: "Michael", surname: "Colin") }
NoMethodError:
undefined method `build' for nil:NilClass
# ./spec/models/profile_spec.rb:23:in `block (2 levels) in <top (required)>'
5) Profile
Failure/Error: before { #profile = user.profile.build(given_name: "Michael", surname: "Colin") }
NoMethodError:
undefined method `build' for nil:NilClass
# ./spec/models/profile_spec.rb:23:in `block (2 levels) in <top (required)>'
6) Profile user
Failure/Error: before { #profile = user.profile.build(given_name: "Michael", surname: "Colin") }
NoMethodError:
undefined method `build' for nil:NilClass
# ./spec/models/profile_spec.rb:23:in `block (2 levels) in <top (required)>'
7) Profile when user_id is not present
Failure/Error: before { #profile = user.profile.build(given_name: "Michael", surname: "Colin") }
NoMethodError:
undefined method `build' for nil:NilClass
# ./spec/models/profile_spec.rb:23:in `block (2 levels) in <top (required)>'
8) Profile accessible attributes should not allow access to user_id
Failure/Error: before { #profile = user.profile.build(given_name: "Michael", surname: "Colin") }
NoMethodError:
undefined method `build' for nil:NilClass
# ./spec/models/profile_spec.rb:23:in `block (2 levels) in <top (required)>'
Finished in 1.56 seconds
29 examples, 8 failures
Failed examples:
rspec ./spec/models/profile_spec.rb:27 # Profile
rspec ./spec/models/profile_spec.rb:28 # Profile
rspec ./spec/models/profile_spec.rb:29 # Profile
rspec ./spec/models/profile_spec.rb:30 # Profile
rspec ./spec/models/profile_spec.rb:33 # Profile
rspec ./spec/models/profile_spec.rb:31 # Profile user
rspec ./spec/models/profile_spec.rb:37 # Profile when user_id is not present
rspec ./spec/models/profile_spec.rb:41 # Profile accessible attributes should not allow access to user_id
Done.
>
.
require 'spec_helper'
describe Profile do
# before do
# This code is wrong but it works
# #profile = Profile.new(given_name: "Michael", surname: "Colin", user_id: user.id)
# end
# but I am attempting to create a profile via User/Profile assoications
let(:user) { FactoryGirl.create(:user) }
before { #profile = user.profile.build(given_name: "Michael", surname: "Colin") }
subject { #profile }
it { should respond_to(:given_name) }
it { should respond_to(:surname) }
it { should respond_to(:user_id) }
it { should respond_to(:user) }
its(:user) { should == user }
it { should be_valid }
describe "when user_id is not present" do
before { #profile.user_id = nil }
it { should_not be_valid }
end
describe "accessible attributes" do
it "should not allow access to user_id" do
expect do
Profile.new(user_id: user_id)
end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
end
has_one will give you a build_profile method on user. See http://guides.rubyonrails.org/association_basics.html#has_one-association-reference for more details.
The reason you can't do user.profile.build is that there is no profile, so user.build returns nil and it doesn't make sense to ask nil to build a profile. This is different to the has_many case, where out always returns a collection of things - even when the collection is empty - and you can ask the collection to make another member. It's easy to imagine the has_one accessor returning a non-nil "I'm not here" value, which would enable your scenario, but would have other issues (mainly that such a value would not be falsey, leading to arguably less rubyesque code)

Rails Tutorial 3 Chapter 7: User Model Rspec Test Failing

I've been following the Rails Tutorial 3 successfully until I got to chapter 7 and implemented the user model, now my rspec keeps failing.
Here's my user.rb file output
class User < ActiveRecord::Base
attr_accessible :name, :email
email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }
before_save :encrypt_password
# Return tue if the user's password matches the submitted password.
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
def self.authenticate(email, submitted_password)
user = find_by_email(email)
return nil if user.nil?
return user if user.has_password?(submitted_password)
end
private
def encrypt_password
self.salt = make_salt unless has_password?(password)
self.encrypted_password = encrypt(password)
end
def encrypt(string)
secure_hash("#{salt}--#{string}")
end
def make_salt
secure_hash("#{Time.now.utc}--#{password}")
end
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
end
Here's my users_spec.rb
require 'spec_helper'
describe User do
before(:each) do
#attr = {
:name => "Example User",
:email => "user#example.com",
:password => "foobar",
:password_confirmation => "foobar"
}
end
it "should create a new instance given a valid attribute" do
User.create!(#attr)
end
it "should require a name" do
no_name_user = User.new(#attr.merge(:name => ""))
no_name_user.should_not be_valid
end
it "should require an email address" do
no_email_user = User.new(#attr.merge(:email => ""))
no_email_user.should_not be_valid
end
it "should reject names that are too long" do
long_name = "a" * 51
long_name_user = User.new(#attr.merge(:name => long_name))
long_name_user.should_not be_valid
end
it "should accept valid email addresses" do
addresses = %w[user#foo.com THE_USER#foo.bar.org first.last#foo.jp]
addresses.each do |address|
valid_email_user = User.new(#attr.merge(:email => address))
valid_email_user.should be_valid
end
end
it "should reject invalid email addresses" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo.]
addresses.each do |address|
invalid_email_user = User.new(#attr.merge(:email => address))
invalid_email_user.should_not be_valid
end
end
it "should reject duplicate email addresses" do
User.create!(#attr)
user_with_duplicate_email = User.new(#attr)
user_with_duplicate_email.should_not be_valid
end
it "should reject email addresses identical up to case" do
upcased_email = #attr[:email].upcase
User.create!(#attr.merge(:email => upcased_email))
user_with_duplicate_email = User.new(#attr)
user_with_duplicate_email.should_not be_valid
end
describe "passwords" do
before(:each) do
#user = User.create!(#attr)
end
it "should have a password attribute" do
#user.should respond_to(:password)
end
it "should have a password confirmation attribute" do
#user.should respond_to(:password_confirmation)
end
end
describe "password validations" do
it "should require a password" do
User.new(#attr.merge(:password => "", :password_confirmation => "")).
should_not be_valid
end
it "should require a matching password confirmation" do
User.new(#attr.merge(:password_confirmation => "invalid")).
should_not be_valid
end
it "should reject short passwords" do
short = "a" * 5
hash = #attr.merge(:password => short, :password_confirmation => short)
User.new(hash).should_not be_valid
end
it "should reject long passwords" do
long = "a" * 41
hash = #attr.merge(:password => long, :password_confirmation => long)
User.new(hash).should_not be_valid
end
end
end
Finally here's the output of my rspec
Failures:
1) UsersController GET 'show' should be successfull
Failure/Error: #user = Factory(:user)
ArgumentError:
Factory not registered: user
# ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
2) UsersController GET 'show' should find the right user
Failure/Error: #user = Factory(:user)
ArgumentError:
Factory not registered: user
# ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
3) User should create a new instance given a valid attribute
Failure/Error: User.create!(#attr)
NoMethodError:
undefined method `password' for #<User:0x007f9d3684e0b0>
# ./spec/models/user_spec.rb:15:in `block (2 levels) in <top (required)>'
4) User should require a name
Failure/Error: no_name_user.should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d36eacf38>
# ./spec/models/user_spec.rb:20:in `block (2 levels) in <top (required)>'
5) User should require an email address
Failure/Error: no_email_user.should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d36e45978>
# ./spec/models/user_spec.rb:25:in `block (2 levels) in <top (required)>'
6) User should reject names that are too long
Failure/Error: long_name_user.should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d36e0b2a0>
# ./spec/models/user_spec.rb:31:in `block (2 levels) in <top (required)>'
7) User should accept valid email addresses
Failure/Error: valid_email_user.should be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d36da4c80>
# ./spec/models/user_spec.rb:38:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:36:in `each'
# ./spec/models/user_spec.rb:36:in `block (2 levels) in <top (required)>'
8) User should reject invalid email addresses
Failure/Error: invalid_email_user.should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d36d870b8>
# ./spec/models/user_spec.rb:46:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:44:in `each'
# ./spec/models/user_spec.rb:44:in `block (2 levels) in <top (required)>'
9) User should reject duplicate email addresses
Failure/Error: User.create!(#attr)
NoMethodError:
undefined method `password' for #<User:0x007f9d36c6c890>
# ./spec/models/user_spec.rb:51:in `block (2 levels) in <top (required)>'
10) User should reject email addresses identical up to case
Failure/Error: User.create!(#attr.merge(:email => upcased_email))
NoMethodError:
undefined method `password' for #<User:0x007f9d36c4d878>
# ./spec/models/user_spec.rb:58:in `block (2 levels) in <top (required)>'
11) User passwords should have a password attribute
Failure/Error: #user = User.create!(#attr)
NoMethodError:
undefined method `password' for #<User:0x007f9d36b3cda8>
# ./spec/models/user_spec.rb:66:in `block (3 levels) in <top (required)>'
12) User passwords should have a password confirmation attribute
Failure/Error: #user = User.create!(#attr)
NoMethodError:
undefined method `password' for #<User:0x007f9d369c27c0>
# ./spec/models/user_spec.rb:66:in `block (3 levels) in <top (required)>'
13) User password validations should require a password
Failure/Error: User.new(#attr.merge(:password => "", :password_confirmation => "")).
NoMethodError:
undefined method `password' for #<User:0x007f9d3699e5f0>
# ./spec/models/user_spec.rb:81:in `block (3 levels) in <top (required)>'
14) User password validations should require a matching password confirmation
Failure/Error: User.new(#attr.merge(:password_confirmation => "invalid")).
NoMethodError:
undefined method `password' for #<User:0x007f9d3698e600>
# ./spec/models/user_spec.rb:86:in `block (3 levels) in <top (required)>'
15) User password validations should reject short passwords
Failure/Error: User.new(hash).should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d3697dda0>
# ./spec/models/user_spec.rb:93:in `block (3 levels) in <top (required)>'
16) User password validations should reject long passwords
Failure/Error: User.new(hash).should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d3696c5a0>
# ./spec/models/user_spec.rb:99:in `block (3 levels) in <top (required)>'
Finished in 0.80301 seconds
35 examples, 16 failures, 2 pending
Failed examples:
rspec ./spec/controllers/users_controller_spec.rb:12 # UsersController GET 'show' should be successfull
rspec ./spec/controllers/users_controller_spec.rb:17 # UsersController GET 'show' should find the right user
rspec ./spec/models/user_spec.rb:14 # User should create a new instance given a valid attribute
rspec ./spec/models/user_spec.rb:18 # User should require a name
rspec ./spec/models/user_spec.rb:23 # User should require an email address
rspec ./spec/models/user_spec.rb:28 # User should reject names that are too long
rspec ./spec/models/user_spec.rb:34 # User should accept valid email addresses
rspec ./spec/models/user_spec.rb:42 # User should reject invalid email addresses
rspec ./spec/models/user_spec.rb:50 # User should reject duplicate email addresses
rspec ./spec/models/user_spec.rb:56 # User should reject email addresses identical up to case
rspec ./spec/models/user_spec.rb:69 # User passwords should have a password attribute
rspec ./spec/models/user_spec.rb:73 # User passwords should have a password confirmation attribute
rspec ./spec/models/user_spec.rb:80 # User password validations should require a password
rspec ./spec/models/user_spec.rb:85 # User password validations should require a matching password confirmation
rspec ./spec/models/user_spec.rb:90 # User password validations should reject short passwords
rspec ./spec/models/user_spec.rb:96 # User password validations should reject long passwords
Any ideas on what's going on? I've been stuck on this for about a week now
You don't have accessible password or password_confirmation properties on your model. Change:
attr_accessible :name, :email
to:
attr_accessible :name, :email, :password, :password_confirmation

Resources