Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

'has_secure_password' issue, User Authentication with Rails

Hello friends, I keep running into the following error message:

myMacBookPro:odot-authentication aaa$ bin/rspec spec/models/user_spec.rb 
....F

Failures:

  1) User#downcase_email downcases an email before saving
     Failure/Error: expect(user.save).to be_truthy
       expected: truthy value
            got: false
     # ./spec/models/user_spec.rb:46:in `block (3 levels) in <top (required)>'

Finished in 0.08466 seconds
5 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:43 # User#downcase_email downcases an email before saving

Randomized with seed 63049

The corresponding file is here:

require 'spec_helper'

describe User do let(:valid_attributes) { { first_name: "Jason", last_name: "Seifer", email: "jason@teamtreehouse.com", password: "treehouse1234", password_confirmation: "treehouse1234" } } context "validations" do let(:user) { User.new(valid_attributes) }

before do 
  User.create(valid_attributes)
end

it "requires an email" do
  expect(user).to validate_presence_of(:email)
end

it "requires a unique email" do
  expect(user).to validate_uniqueness_of(:email)
end

it "requires a unique email (case insensitive)" do
  user.email = "JASON@TEAMTREEHOUSE.COM"
  expect(user).to validate_uniqueness_of(:email)
end

end

describe "#downcase_email" do it "makes the email attribute lower case" do user = User.new(valid_attributes.merge(email: "JASON@TEAMTREEHOUSE.COM")) expect{ user.downcase_email }.to change{ user.email }. from("JASON@TEAMTREEHOUSE.COM"). to("jason@teamtreehouse.com") end

it "downcases an email before saving" do
  user = User.new(valid_attributes)
  user.email = "MIKE@TEAMTREEHOUSE.COM"
  expect(user.save).to be_truthy
  expect(user.email).to eq("mike@teamtreehouse.com")
end

end end

Any suggestions? Thanks so much!

In line 46 is expect(user.save).to be_truthy

43: it "downcases an email before saving" do

44:user = User.new(valid_attributes)

45:user.email = "MIKE@TEAMTREEHOUSE.COM"

46:expect(user.save).to be_truthy

47:expect(user.email).to eq("mike@teamtreehouse.com")

48:end

2 Answers

With apologies for the formatting...

Brandon Barrette
Brandon Barrette
20,485 Points

In my ODOT code, I have "expect(user.save).to be_true" and not to be_truthy

I had that as well, however I received a deprecation warning about using be_true (indicating to use be_truthy instead, same with be_false --> be_falsey). https://github.com/rspec/rspec-expectations/issues/283 I think what I'll do is restart that particular module, retrace my steps, and see what might have gone wrong. Thanks!

Brandon Barrette
Brandon Barrette
20,485 Points

Are you using Rspec 3? That is change from the videos which use some from of Rspec 2. I know in Rspec 3 there are some changes that will throw errors your way.

Using be_true or be_truthy gives the same error that I'm now faced with. Whether the tests fails because it expected: true, got: false or exected: truthy value, got: false is semantics. Indeed the deprecation was done because be_true was acting like be_truthy already.

With either bit of code, the expect(user.save).to be_true fails on the addition of has_secure_password makes this fail - presumably, there's some step to take before a user can be saved when using BCrypt, I'm not sure!

I can easily remove/amend the test if we can't get to the bottom of it, but I thought I'd flag this again. It is probably best on a separate thread, though!

Steve.