RailsCasts Pro episodes are now free!

Learn more or hide this

Tom van leeuwen's Profile

GitHub User: TvL2386

Site: http://blog.vleeuwen.eu

Comments by Tom van leeuwen

Avatar

I can't get the timecop to work in my application.
I hope you guys know what's wrong. I've used Ryans code.
What I see is that the reloaded object has nil for the password_reset_sent_at attribute.

I get:
1) User#send_password_reset saves the time the password reset was sent
Failure/Error: user.reload.password_reset_sent_at.should eq(Time.zone.now)

   expected Fri, 31 Dec -0001 23:40:28 UTC +00:00
        got nil

   (compared using ==)
 # ./spec/models/user_spec.rb:24:in `block (3 levels) in <top (required)>'
ruby
# user_spec.rb
    it "saves the time the password reset was sent" do
      Timecop.freeze
      user.send_password_reset

      puts 'printing user password_reset_sent_at'
      p user.password_reset_sent_at
      puts 'printing reloaded user password_reset_sent_at'
      p user.reload.password_reset_sent_at
      puts 'printing last user password_reset_sent_at'
      p User.last.password_reset_sent_at
      user.reload.password_reset_sent_at.should eq(Time.zone.now)
    end

# user.rb (model)
  def send_password_reset
    generate_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

stdout:
printing user password_reset_sent_at
Fri, 31 Dec -0001 23:40:28 UTC +00:00
printing reloaded user password_reset_sent_at
nil
printing last user password_reset_sent_at
nil

Avatar

Hi Ryan,

I've been trying and trying to start with testing, but it's very easy to get lost. Thanks for this great railscast! I will definately pick up on testing again!
I watch all your eps and love them!