Hey, I have service and EDM model - Model1Container :
public class Service1 : System.Web.Services.WebService
{
Model1Container modelDataBase = new Model1Container();
[WebMethod]
public List<Person> getData()
{
using (modelDataBase = new Model1Container())
{
var a = from aa in modelDataBase.Person
select aa;
return a.ToList();
}
}
}
and I want to test this web service with nunit. But I find problem - in service I use model from production database, but in test I want to test service with development database.
How can I independ service from database ?
public class TestClass
{
Model1Container model = new Model1Container();
[TestFixtureSetUp]
public void SetUp()
{
using (Model1Container model = new Model1Container())
{
Person p = new Person()
{
name = "Michal",
surname = "Nowak",
age = 12,
开发者_C百科 dateOfBirth = new DateTime(1987, 12, 3)
};
model.AddToPerson(p);
model.SaveChanges();
}
}
[Test]
public void TestGetData()
{
WebService1.Service1 service = new WebService1.Service1();
var actual = service.getData();
Assert.AreEqual(1, actual.Count);
}
}
Add a property setter to your service and inject your own model container before executing the test.
You would also need to refactor your service so that it doesn't init the container in the service method. (Or only inits if it's null)
精彩评论