Should every function have a unit test?

Should every function have a unit test?

The answer to the more general question is yes, you should unit test everything you can. Doing so creates a legacy for later so changes down the road can be done with peace of mind. It ensures that your code works as expected. It also documents the intended usage of the interfaces.

Should unit tests make external calls?

Your unit test should not test whether the external webservice is working, or whether your integration with it is correct. Without getting too dogmatic about TDD, note that a side effect of turning your unit test into an integration test is that it’s likely to run slower, and you want fast unit tests.

What makes a unit test for a function good?

Good unit tests should be reproducible and independent from external factors such as the environment or running order. Fast. Developers write unit tests so they can repeatedly run them and check that no bugs have been introduced.

Can you unit test API calls?

An API test is similar to a unit test with a setup (prepare data for calling the API method), a call to the API method and then checking of the results. The semantics may require multiple calls to the API, producing tests that have more than one step.

How to unit test methods that call other methods inside same?

Also allows for testing if a static Func was called from another method since static methods can not be mocked. Easy to refactor existing methods to Funcs/Actions since the syntax for calling a method on the call site remains the same. (For times when you can’t refactor a method into a Func/Action see cons)

When do you use mocks in unit tests?

In general we use mocks for external dependencies/other object/interface calls being used within our class for which we will write unit tests. So when you are writing test for one of your function which internally makes a call to another function within the same class you don’t have to mock that function call.

When to use testvalue when calling another method?

If it mutates the original value reference instead of returning it, you can just use testValue as the actual value after the call occurs. When testing boundary conditions like if (value.length > 5) you should make sure your test data contains values of value that have length 4, 5, or 6.

What are the pros and cons of unit test methods?

The biggest Con is that you don’t have access to the ‘this’ reference in Funcs and Actions, and so any dependencies on the class must be explicitly passed in as parameters. Its good as you know all dependencies but bad if you have many properties that your Func will depend on. Your mileage will vary based on your use case.