dimanche 24 janvier 2016

Writing tests for user authentication with rspec

I am currently trying to write unit tests for user authentication in rails and I keep running into a problem.

I am trying to test the following method:

def reset_session_token!
  self.session_token = User.generate_session_token
  self.save!
  self.session_token
end

with the following unit test:

let(:valid_user) { User.new(user_name: 'Name', password: 'abcdefghijkl')}

it "should set the session_token" do     
  valid_user.reset_session_token!

  expect(valid_user.session_token).not_to be_nil
end

but the test fails with the error Validation failed: User name has already been taken. I suspect that it is because reset_session_token! calls save on the user instance but this is necessary for the method to work properly. How can I get around this?

Aucun commentaire:

Enregistrer un commentaire