Reputation: 565
I am working on adding some new validation to an app. The entire idea is to make sure that when a user updates their username it does not violate our username policy.
This is the current incantation of the validator:
validates_format_of :username, with: /[0-9a-zA-Z_]+/,
on: :update,
if: lambda { |u| u.username_changed? }
Even with this validation bad characters make it through.
Here is my spec that I am using:
it "validates and does not update a user with an invalid username" do
user.update_attributes(username: "k~!tten")
expect(user.username).not_to eq "k~!tten"
end
Any help with this is greatly appreciated.
Upvotes: 0
Views: 66
Reputation: 10693
Username is "k~!tten" only on the model. It has not been saved to the database due to validation failure. Instead of:
expect(user.username).not_to eq "k~!tten"
use the below to assert that the username does not pass validation:
expect(user.username).not_to be_valid
Upvotes: 1