I'm using Clearance for authentication in my Rails application.
I'm using the default Minitest stack. Clearance by default only use email and password fields so i add a name field and now i want to unit test the model to do some checks.
This is my test so far.
test "should not save without name" do
user = User.new
user.email = "[email protected]"
user.password = "foobar"
assert_not user.valid?
end
And this is my model validation.
validates :name, presence: true
Everything works great and the test fails as expected. The problem is that i wanted to use a user fixture to clean a little my test.
One viable approach could be
john:
name: John
email: [email protected]
password: foobar
But then the test execution fails saying that there isn't a field called password. Indeed the field is called encrypted_password. I can create one using the BCrypt::Password.create method but again, another error raises telling me that remember_token can't be null, i suspect that the same happens with the confirmation_token field.
So my question is.
Is there any way that i can create a rails fixture to use those data in my test using the clearance authentication library ?
Update: To clarify the fixture example due to the correct answer the result fixture is:
john:
name: John
email: [email protected]
encrypted_password: <%= BCrypt::Password.create("foobar", cost: 4) %>
remember_token: <%= Clearance::Token.new %>
And that's it, it works like charm.
When the User ActiveRecord API is used to create the object, all of the required fields should be set appropriately. Unfortunately, it doesn't look like this happens with fixtures - or at least it's not happening for you (I don't use fixtures, so I'm not sure).
You can set any required tokens to: Clearance::Token.new, which is what Clearance uses to generate tokens.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With