I've been following along Hartl's book at rubystutorial.org and stuck on this error
When I run rspec tests it tells me that the method (save) does not exist for user
Here's my code and test. I can't find what's wrong with it and why the method doesn't exist within @user
user_spec.rb
require 'spec_helper'
describe User do
before do
@user = User.new(name: "Example User", email: "[email protected]")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should be_valid }
describe "when name is not present" do
before { @user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "shoud be invalid" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo. foo@bar_baz.com foo@bar+baz.com]
addresses.each do |invalid_address|
@user.email = invalid_address
expect(@user).not_to be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[[email protected] [email protected] [email protected] [email protected]]
addresses.each do |valid_address|
@user.email = valid_address
expect(@user).to be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
end
And my user.rb
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates(:name, presence: true, length: { maximum: 50 } )
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
Exact error is
Failures:
1) User when email address is already taken
Failure/Error: user_with_same_email.save
NoMethodError:
undefined method `save' for "[email protected]":String
# ./spec/models/user_spec.rb:55:in `block (3 levels) in <top (required)>'
Finished in 0.411 seconds
24 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:58 # User when email address is already take
You are defining a user and then change it on string (email, so email has no save method) in the next lines:
user_with_same_email = @user.dup
user_with_same_email = @user.email.upcase
user_with_same_email.save
You should change it for this:
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save