views:

66

answers:

3

Hello,

So in my domain I have 3 objects. User, Item and Tag. Each user has items and tags, each item has tags and each tag has items.

The objects look a little like this:

public class User
{
    public List<Item> Items { get; set; }
    public List<Tag> Tags { get; set; }
}
public class Item
{
     public List<Tag> Tags { get; set; }
}
public class Tag
{
     public List<Item> Items { get; set;}
}

This is the first time I have a go TDD so I was wondering how do I get mocks/fakes(not sure about the right term) that I can use in my tests. At first I tried to just create some fake objects but with the many to many it seems like a lot of work.

Thanks in advance

+1  A: 

Generally you would create an interface for all of those use those interfaces with a Mock framework:

public interface IUser
{
    List<Item> Items { get; set; }
    List<Tag> Tags { get; set; }
}
public interface IItem
{
     List<Tag> Tags { get; set; }
}
public interface ITag
{
     List<Item> Items { get; set;}
}

If, for example, you use NMock, you would something like this:

IUser user = mockery.NewMock<IUser>();
var testTags = new List<ITag.();
user.Expect().Once.On("Tags").Will(Return.Value(testTags));

This technique allows you to slice your components and place mock object and use those for testing. Also the mock framework will create temporary implementations of the interface which is very convinient.

As always, when you start with TDD and mocking, you must read this Martin Fowler article:

http://martinfowler.com/articles/mocksArentStubs.html

Igor Zevaka
A: 

I'm not sure if I've understood your question fully. I would think you are refering to the mock/test data and not the TDD framework/methodology?

If as such, I would suggest you do it in a semi-auto manner.

Since a Tag can be associated with User and Item, create a pool of them programmatically (random string or from a set of keywords).

Next is the Item. Create a pool of them linking to 0 (or 1?) to N Tags. You might have to add some random element or pattern for variation.

Lastly, create the list of Users. Link them to Items and Tags similar to how you do it for Item.

Depending on your domain model, you might want to create orphan Items and Tags as well.

o.k.w
I need some test data to write some tests against I guess. I am very new to this so I probably am using the wrong words.
Pickels
+1  A: 

User, Item and Tag don't look like objects, they look like structs, so I'd just use real Data objects here and not mocks. Mocks work better when they're used to isolate behaviour (asking a collaborating object to do something for you). Try a look at Rebecca Wirf-Brocks book on Object Design.

Steve Freeman
OK, thanks, will check it out.
Igor Zevaka