简体   繁体   中英

How do I test properly?

For example I would like to validate that name is letters only and is between 4 and 14 letters length. I have the following code in model:

    validates: name, :format => { :with => /^[a-zA-Z]+$/,
                                  :message => 'Name should be letters only. },
                     :length => { :minimum => 4, :maximum => 14 }

So it clearly lets me do what I want.
But as for unit tests, I have a bit too much perfectionism so I set something like

invalid_names = ['1234',
                 'qwe',
                 '1%#$#$',
                 'Sam1',
                 '%',
                 random_string(15)] #I also have a test method to create random string with parametrized length

valid_names = %w['test',
                 'Travis',
                 'John',
                 random_string(5),
                 random_string(14),
                 random_string(4)]

and test each of them in a loop with asserts, like

invalid_names.each do |name|
    user = User.new(:name => name)
    user.save
    assert user.errors[:name].any?, "#{name} is valid."
end

So it definitely works great. But it is too verbose, also I can't be sure my test actually tests all symbols and their combinations possible and all lengths and stuff, even though I am definitely sure it works as expected.
So what is acceptable way to test my validation without being too much perfectionist, yet to leave most part of logic tested?
Am I just set in a mind trick of trying to write a perfect code just to write a perfect code and forgetting about main finish goal: working product?

I think of it this way: what are you trying to test? How certain are you of your regex (which in this case is drop-dead simple)?

The random string test is pointless, IMO, and meaningless to a reader because the generation function is poorly-named in that it doesn't indicate it will only generate names that match the regex, which I can glean from context, but it's still as assumption .

TDD people will punch me in my face, but I'm not convinced leaving in these tests after initial development has lasting value. I'm not even convinced having tests for this ever has value, because the logic is trivial, and it strikes me as being more about testing Rails than my validation.

Delta my comments about the string generation method, however, I don't have any issues with the tests you show. If there are going to be tests, this is a reasonable way to write them, it's easy to add use cases to either, and except for a minor rise in test time, they do no harm.

No matter how much you test, you'll never reach path coverage once a program gets a little more complicated. So rather than trying to test any given combination, try to test every combination which is structurally different from the ones already tried. So for example, if you got an algorithm which is taking three ints and constructs a triangle with the ints as lengths of sides, try only one possible solution but try an impossible solution for every reason a solution might wrong, for the triangle this might be non-numeric inputs, but also triangles which do not fulfill the Pythagorean theorem. So for your example, do not test for random strings, but for null, overflows, special characters and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM