简体   繁体   English

从数据库/ EDM模型中独立服务-通过nunit测试Web服务

[英]independ service from database / EDM model - test web service by nunit

Hey, I have service and EDM model - Model1Container : 嘿,我有服务和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();
            }
        }
}

and I want to test this web service with nunit. 我想用nunit测试此Web服务。 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);
        }
    }

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. 您还需要重构服务,以便它不会在service方法中初始化容器。 (Or only inits if it's null) (或者只有init,如果它为null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM