ruby - Rails assert that form is valid -
What is the best practices to test in a way that is valid in a model rail?
For example, if I have a user model that validates the specificity of an email / addase property, how do I see the error of posting the form (or better yet, especially that An error has been returned for the field).
I think something should be clear, but as I'm quickly searching, I still do not have enough vocabulary to do the Ruby questions effectively.
The easiest way would be:
class UserEmailAddressDuplicateTest & lt; ActiveSupport :: testcase def setup @email = "test@example.org" @ user1, @ user2 = User.create (: email = & gt; @email), User.new (: email = & gt; @email) end Def test_user_should_not_be_valid_given_duplicate_email_addresses emphasize! End def test_user_should_produce_error_for_duplicate_email_address # Testing for the default error message: Assert_equal "Already taken", @ user2.errors.on (: Email) And End
Of course it is possible that you If you do not want to create a separate test case for practice, in that case you can duplicate the argument in the setup method and include it in both tests (or keep it in a personal method).
Alternatively, you can store the first (reference) user in a stabilizing fixtures / users.yml
, and just in a new user with a duplicate address in each exam As an example.
Refactor as you see fit!
Comments
Post a Comment