Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

client_test: Tests are not valuable #116

Open
ybakos opened this issue May 2, 2018 · 4 comments
Open

client_test: Tests are not valuable #116

ybakos opened this issue May 2, 2018 · 4 comments
Labels

Comments

@ybakos
Copy link
Member

ybakos commented May 2, 2018

The tests are merely re-asserting the obvious. You are instantiating a valid client and then asserting that it is valid, why saying " with a first name is valid.

  test "Client with a first name is valid" do
    valid_client = clients(:valid)
    assert(valid_client.valid?)
  end

This is a "no duh" test. Instead, test that a valid client that then has an invalid name is considered invalid.

  test "without a first name is invalid" do
    client = clients(:valid)
    assert(client.valid?)
    client.first_name = nil
    refute client.valid?
  end

Same for the other tests.

@bnickodemus
Copy link
Contributor

That is literally the same thing. The only thing different is that you show an invalid name is invalid, also a no duh test. The tests would be useful if client.rb had any interesting methods but it doesn't so there are no interesting tests.

@ybakos
Copy link
Member Author

ybakos commented May 9, 2018

@bnickodemus It is not the same thing. Look at the first test. You are explicitly setting valid attributes and then asserting that the client is valid. Because you are explicitly setting valid attributes, the test does not prove anything.

Now look at the second test. It is entirely different. First, you are asserting that the client is in a valid state, before making a single change to an attribute that will make the client invalid. By explicitly changing only first_name to nil, and then refuting its valid?-ness, you are asserting that "a client without a first name is invalid."

Prove it to yourself: remove the first_name validator in the Client model and see that the first test still passes but the second test would fail, because the record would be valid, but you are asserting that it should not be valid.

@bnickodemus
Copy link
Contributor

Hi Yong,
Could you please provide another example.
I don't think I understand what a valuable test is.

@ybakos
Copy link
Member Author

ybakos commented May 16, 2018

@bnickodemus Glad you asked! First, do you see the difference in the previous tests above? Do you see how the first test isn't valuable, but the second test is?

Also, look at client_test.rb. Some notes:

  • "should not save client without name": No, this is testing the AR machinery. You should only be testing for a valid/invalid object - Rails ActiveRecord will prevent the saving. This test can be deleted.
  • "dob should be a date": I wouldn't test that either. AR will take care of that. Perhaps just validate that a client responds to dob. The test is "a client has a date of birth."
  • "ascii charaters are invalid in insurance_company": This test is not well-named, nor is the fixture. All characters are ascii. What are you testing?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants