Mocking Objects for Unit Tests

Possible duplicate:
Hard-coded breadboard objects against a torn frame

I think that, finally, I am beginning to understand what unit tests are designed to solve, but I still have problems with the implementation of all the details. I came to the conclusion that I might need a “layout” (and I use this term easily because I’m not sure that I need an object such as Moq) to complete the task.

As an example of the problem I encountered, consider the implementation of a repository template (or similar). As I understand it now, I need (at least) tests for each of the methods of the class Add(), Get()and Remove(). This is normal, except that I want to check how the method Add()handles the links null. In this case, I would simply define a simple class in a test project and set an instance of this value nullin within the corresponding unit test?

Unit Test Example (Illustration):

[TestMethod]
public void TestAdd_Null()
{
    IRepository<MockObject> repository = (IRepository<MockObject>)(new Repository<MockObject>());
    MockObject testObject = null;

    repository.Add(testObject);

    Assert.IsNotNull(repository.Entity);
}

// I'm thinking I should implement something like this exclusively within the Test project.
// Is this reasonable? Or should I be looking into something else?
internal class MockObject
{
    public String Name { get; set; }
}
+3
source share
1 answer

, The Matrix. , , . . !

, , . , , -.

Mocks , , . , . , , , . , , .

, Moq, , , , . , Entity , - ( , , , ). , .

- , , , , factory . , , . , .

, , , .

+1

All Articles