Reputation: 14316
I'm trying to learn writing unit tests. I followed the repository pattern and created a repository for my controller. Is supports both real and fake dbcontexts and now I'm ready to start writing tests. My problem is that I don't know what to test.
The most important thing. Can you give me some examples of unit test for asp mvc? I suppose that a list of statements like [method]ShouldDo[whatever] might be helpful. But I'm afraid it may be hard come up with something not so obvious offhand. Maybe there is somebody willing to share one of their test files/projects or recommend an open source project worth taking a look at?
public virtual void Update(T entity)
{
Db.Attach(entity);
Db.SaveChanges();
}
public virtual void Save(T entity)
{
Db.Add(entity);
Db.SaveChanges();
}
public virtual void Remove(T entity)
{
Db.Delete(entity);
Db.SaveChanges();
}
Upvotes: 0
Views: 504
Reputation: 101194
- Should I test the controller if most of it's job is to pass data to the repository and than pass the the result to the view?
A controller per definition should only be an adapter between the model and the view. Hence it should not process (i.e. business rules) the information.
sounds like your controller doesn't, which is all good. if you are using view models there's only one thing that can go wrong: And that's that you don't handle ModelState properly (i.e. report back if something is wrong).
So if you want to test that, go ahead.
In my repository I have methods like below. They are quite straightforward, should they be tested?
No. Since you do not validate that the incoming data is correct. The tests would just be integration tests and not unit tests.
Should I test conditions of validation?
If you would like to make sure that you validate what should be validated.
The most important thing. Can you give me some examples of unit test for asp mvc? I suppose that a list of statements like [method]ShouldDo[whatever] might be helpful. But I'm afraid it may be hard come up with something not so obvious offhand. Maybe there is somebody willing to share one of their test files/projects or recommend an open source project worth taking a look at?
Depends on what you would like to test.
Upvotes: 1
Reputation: 562
Unfortunately, this topic is way too broad to be given a simple answer here. Google is your friend. I googled "writing unit tests for c#" and got over 2 million responses. There are many competing philosophies about how and when to write unit tests. I'd suggest that you research - starting on google - read several of the approaches and decide, based on your own preferences, coding style, and the specific problem that you are trying to solve which would be the best fit.
Upvotes: 1