繁体   English   中英

此单元测试EF出了点问题

[英]Somthing wrong with this Unit Test EF

单元测试和EF代码中的第一个新功能是尝试使用EF将数据添加到数据库的功能。 我不知道我的代码出了什么问题,它应该会失败,但是通过了这是m Test函数

[TestClass]
public class ProspectControllerTest
{
    [TestMethod]
    public void Index()
    {     
     DataContext context = new DataContext();
           Seller c = new Seller
           {
               SellerName = "test20",
               SellerDivision = "test20",
               SellerCity  = "City410",
               Active = true,

           };
        context.sellers.Add(c);

           context.SaveChanges();


           Seller s = (from d in context.sellers
                       where d.SellerId == c.SellerId
                       select d).Single();
           //This shouldent pass because there is no records in the db !
           c.SellerName.Equals(s.SellerName);

       }
    }
}

这是M班

  public class Seller
{
    [Key]
    public int SellerId { get; set; }
    public string SellerName { get; set; }
    public string SellerDiv { get; set; }
    public string SellerCity { get; set; }

    public Boolean Active { get; set; }

    public Div Div { get; set; }
    public ICollection<Prospect> prospects { get; set; }
}

另一个问题是,此测试是否将任何数据存储到db中?我应该能够保存数据库中的记录吗?

更改以下行

//This should fail
c.SellerName.Equals("asdfsadfasdfsadfsadf");

Assert.AreEqual<string>("asdfsadfasdfsadfsadf", c.SellerName);

断言语句验证您的测试输出。

如果您要验证是否在数据库中插入了正确的对象,请使用以下assert语句进行验证。

Assert.IsNotNULL(s);
Assert.AreEqual<string>("asdfsadfasdfsadfsadf",c.SellerName);

您必须在测试方法中使用Mock来执行类似插入数据库的操作( 但不会在用于运行代码的数据库中插入查询,但由于使用Mock不会将其保存在数据库中 )。

注意:使用MOCK概念可实现此目的。

Assert.AreEqual(c.SellerName, "asdfsadfasdfsadfsadf");

并使用断言检查值。

暂无
暂无

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

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