0

I have made the pragmatic decision to have my repository serve as both a repository and a a severely thin service layer.

One method of the repository that I want to test is the Add method. It looks like this:

public void Add(Post post) { post.Slug = SlugConverter.Convert(post.Title); context.Posts.Add(post); context.SaveChanges(); } 

What I want to test in particular is that the Add method somehow updates the Slug property of the given post.

How should I go about testing this? I wonder how specific I should be given that the SlugConverter is already amply tested.

2 Answers 2

1

You have to use a mocking framework to test this functionality. Using Moq:

[Test] public void Add_PostIsValid_PostSlugIsUpdated() { var post = new Mock<Post>(); post.Setup(x=>x.Title).Returns("Test"); var myRepository = new MyRepository(); myRepository.Add(post.Object); post.VerifySet(pst => pst.Slug = SlugConverter.Convert(post.Title)); } 

Also try to lose dependencies for easier and more robust testing. Treat this code snippet as pseudo code as I don't have access to visual studio right now.

Sign up to request clarification or add additional context in comments.

3 Comments

This requires a access to a database. The call to myRepository.Add(post.Object) will call SaveChanges() which will execute SQL on a database. I my opinion, you are mocking the wrong class
That's true, the "Also try to lose dependencies for easier and more robust testing" part addresses this concern. And I don't think that I'm mocking the wrong class.
Yeah, the context should be mocked as well (not instead of). And I'm not sure how you can really lose the dependancy in this case. Its job is to save stuff to the database. You can make sure the database context is passed into the constructor but it may well already be for all we know and you can't get much better than that.
0

From your description I assume, that whenever Post is being added, you want to make sure Slug property is modified. You don't care if it was properly modified or not (in other words what value it has after being converted using SlugConverter, since, as you mentioned, you already have that part covered), all you care about is that it was modified.

Having said this, you would have

[Test] public void EnsureSlugIsModifiedBeforeBeingSaved() { var post = new Post(); var clonedPost = (Post)post.Clone(); var myRepository = new MyRepository(); myRepository.Add(post); // at this point post should have been saved and Slug should have been modified. Assert.AreNotEqual(clonedPost.Slug, post.Slug); } 

2 Comments

You can't really assume that Post has a working Clone method. You can however just store the slug of the post rather than cloning the whole post.
@Chris good point, however I was trying to give an idea, what should be tester rather than how test method implementation should be like.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.