Unit testing of invalid methods / bullying of subject signals

When a module tests a codebase, what are the telltale signs that I need to use mock objects?

Would it be as simple as seeing a lot of calls to other objects in a codebase?

Also, how would unit test methods that do not return values? So if I return void but prints to a file, am I just checking the contents of the file?

Taunts external dependencies, so literally everything, no? File system, db, network, etc.

+6
unit-testing mocking
source share
3 answers

Anyway, I'm probably over using mocks.

Whenever a class makes a call to another, I usually mock that call, and I verify that the call was made with the correct parameters. Else where, I will have a unit test, which checks if the specific code of the object of the scoffing object is working correctly.

Example:

[Test] public void FooMoo_callsBarBaz_whenXisGreaterThan5() { int TEST_DATA = 6; var bar = new Mock<Bar>(); bar.Setup(x => x.Baz(It.Is<int>(i == TEST_DATA))) .Verifiable(); var foo = new Foo(bar.Object); foo.moo(TEST_DATA); bar.Verify(); } ... [Test] public void BarBaz_doesSomething_whenCalled() { // another test } 

For me, the thing is that if I try to check many classes as one big globe, then usually there are tons of customization code. Not only is it pretty confusing to read when you try to get around all the dependencies, it is very fragile when you need to make changes.

I prefer small brief tests. Easier to write, easier to maintain, easier to understand the purpose of the test.

+2
source share

Mocks / stubs / fakes / test double / etc. well versed in unit tests and allow testing the tested class / system in isolation. Integration tests may not use any layouts; they actually end up in a database or other external dependency.

You use a layout or blank when you need to. This is usually due to the fact that the class you are trying to test depends on the interface. For TDD, you want to program interfaces, not implementations, and use dependency injection (generally speaking).

A very simple case:

 public class ClassToTest { public ClassToTest(IDependency dependency) { _dependency = dependency; } public bool MethodToTest() { return _dependency.DoSomething(); } } 

IDependency is an interface, possibly with expensive calls (access to a database, calls to web services, etc.). The test method may contain code similar to:

 // Arrange var mock = new Mock<IDependency>(); mock.Setup(x => x.DoSomething()).Returns(true); var systemUnderTest = new ClassToTest(mock.Object); // Act bool result = systemUnderTest.MethodToTest(); // Assert Assert.That(result, Is.True); 

Please note that I am doing state testing (as @Finglas suggested), and I am only arguing against the system under test (an instance of the class I'm testing). I can check the property values ​​(state) or the return value of the method, as this case shows.

I recommend reading The Art of Unit Testing , especially if you are using .NET.

0
source share

Unit tests are designed for only one code that works autonomously within itself. This means that it is independent of other objects in order to carry out its work. You must use mocks if you are programming using Test-Driven or Test-First. You will create a layout (or a dummy, as I like to call it) of the function you create, and set certain conditions for the test to pass. The function initially returns false, and the test fails, which is expected ... then you write code to do the real work until it passes.

But I think you mean integration testing, not unit testing. In this case, you should use mocks if you expect other programmers to finish their work and you do not have access to the functions or objects that they create. If you know an interface that I hope you make fun of otherwise, are pointless and a waste of time, then you can create a dead end version of what you hope to get in the future.

In short, layouts are best used when you are waiting for others and need something there to finish your work.

You should try to always return a value, if possible. Sometimes you run into problems when you are already returning something, but in C and C ++ you can have output parameters and then use the return value to check for errors.

-one
source share

All Articles