i’m creating a shopping website that will sell computer parts using MVP and asp.net. i’m trying to unit test a model in Bussiness layer which will insert some values into db using Entity model.
public class CategoryModelRepsitory : IModelRepository
{
public void Insert(string catName, long catParent)
{
EntityContext con = new EntityContext();
Category cat = new Category();
cat.Name = catName;
cat.Parent = catParent;
con.Category.AddObject(cat);
con.SaveChanges();
}
//other methods like update and delete
}
so how do i unit test this and verify the expectations using built-in visual studio test classes?
You can use Unity ( http://unity.codeplex.com/ ) or NInject ( http://www.ninject.org/ ) + Moq ( http://code.google.com/p/moq/ ) to allow your architecture to be easily tested, for example:
After that you’ll be able to mock the IDataContext in your tests and check if some methods with needed parameters were called
p.s.
By the way I’m not sure that especially this will be even compilable, but approach should be like that.
In addition I’d recommend you to write some abstract IRepository class and access database using it
This will also add more testability to your project.
(it’s very bad approach to create the DataContexts inside the method, it should be one for one client or thread + should be calcualted using DI + IoC principles)