Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this particular test could be useful?

Tags:

c#

tdd

I recently joined a company where they use TDD, but it's still not clear for me, for example, we have a test:

[TestMethod]
public void ShouldReturnGameIsRunning()
{
    var game = new Mock<IGame>();
    game.Setup(g => g.IsRunning).Returns(true);
    Assert.IsTrue(game.Object.IsRunning);
}

What's the purpose of it? As far as I understand, this is testing nothing! I say that because it's mocking an interface, and saying, return true for IsRunning, it will never return a different value...

I'm probably thinking wrong, as everybody says it's a good practice etc etc... or is this test wrong?

like image 316
user1082693 Avatar asked Dec 06 '11 02:12

user1082693


People also ask

What is the purpose of particular UP test?

A partial curl up is a test to determine a person's strength and endurance. It has simple procedures that can be completed without much stress, such as one sit-up every three seconds. Even if an audiotape or metronome is not always available, the test is simple, quick, and easy to perform.

What particular physical fitness test would you use to measured muscular endurance?

Muscular endurance tests using calisthenic-type exercises are convenient to use. The curl-up and push-up are the most common calisthenic-type muscular endurance tests used by fitness professionals.

What is the importance of the push up test?

Purpose: This test measures muscular endurance of the upper body muscles (anterior deltoid, pectoralis major, triceps). Muscular endurance is defined as the ability to contract the muscle repeatedly over a specific period of time without undue fatigue.

Why is muscular strength test important?

Greater muscular strength allows an individual to potentiate earlier and to a greater extent, but also decreases the risk of injury. Sport scientists and practitioners may monitor an individual's strength characteristics using isometric, dynamic, and reactive strength tests and variables.


2 Answers

This test is excerising an interface, and verifying that the interface exposes a Boolean getter named IsRunning.

This test was probably written before the interface existed, and probably well before any concrete classes of IGame existed either. I would imagine, if the project is of any maturity, there are other tests that exercise the actual implementions and verify behavior for that level, and probably use mocking in the more traditional isolationist context rather than stubbing theoretical shapes.

The value of these kinds of tests is that it forces one to think about how a shape or object is going to be used. Something along the lines of "I don't know exactly what a game is going to do yet, but I do know that I'm going to want to check if it's running...". So this style of testing is more to drive design decisions, as well as validate behavior.

Not the most valuable test on the planet, but serves its purpose in my opinion.

Edit: I was on the train last night when I was typing this, but I wanted to address Andrew Whitaker comment directly in the answer.

One could argue that the signature in the interface itself is enough of an enforcement of the desired contract. However, this really depends on how you treat your interfaces and ultimately how you validate the system that you are trying to test.

There may be tangible value in explicitly stating this functionality as a test. You are explicitly verifying that this is a desired functionality for an IGame.

Lets take a use case, lets say that as a designer you know that you want to expose a public getter for IsRunning, so you add it to the interface. But lets say that another developer gets this and sees a property, but doesn't see any usages of it anywhere else in the code, one might assume that it is eligible for deletion (or code-rot removal, which is normally a good thing) without harm. The existence of this test, however, explicitly states this properity should exist.

Without this test, a developer could modify the interface, drop the implementation and the project would still compile and run seemingly correctly. And it would be until much later that someone might notice that interface has been changed.

With this test, even if it appears as if no one is using it, your test suite will fail. The self documenting nature of the test tells you that ShouldReturnGameIsRunning(), which at the very least should spawn a conversation if IGame should really expose an IsRunning getter.

like image 200
J. Holmes Avatar answered Oct 10 '22 23:10

J. Holmes


The test is invalid. Moq is a framework used to "mock" objects used by the method under test. If you were testing IsRunning, you might use Mock to provide a certain implementation for those methods that IsRunning depends on, for example to cause certain execution paths to execute.

People can tell you all day that testing first is better; you won't realize that it actually is until you start doing it yourself and realize that you have fewer bugs with code you wrote tests for first, which will save you and your co-workers time and probably make the quality of your code higher.

like image 43
Andrew Whitaker Avatar answered Oct 11 '22 00:10

Andrew Whitaker