独立于数据库/EDM 模型的服务 - 通过 nunit 测试 Web 服务
嘿, 我有服务和 EDM 模型 - 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();
}
}
}
我想用 nunit 测试这个 Web 服务。但我发现问题 - 在服务中我使用生产数据库中的模型,但在测试中我想使用开发数据库测试服务。
如何将服务独立于数据库?
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,
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);
}
}
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,
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在执行测试之前向您的服务添加属性设置器并注入您自己的模型容器。
您还需要重构您的服务,以便它不会在服务方法中初始化容器。 (或者仅在它为空时才初始化)
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)