Entity framework 4 为什么MSpec在使用Entity Framework 4时报告为假阳性?

Entity framework 4 为什么MSpec在使用Entity Framework 4时报告为假阳性?,entity-framework-4,mspec,Entity Framework 4,Mspec,我正试图针对一些EF4对象编写一些Mspec测试。然而,他们正在返回假阳性。我不知道这是我写测试的方式,还是有其他的事情在进行。测试等级如下: [Subject(typeof (Product))] public class When_loading_a_known_product { protected static Product product; protected static ProductDbContext dbContext; Establish con

我正试图针对一些EF4对象编写一些Mspec测试。然而,他们正在返回假阳性。我不知道这是我写测试的方式,还是有其他的事情在进行。测试等级如下:

[Subject(typeof (Product))]
public class When_loading_a_known_product 
{
    protected static Product product;
    protected static ProductDbContext dbContext;

    Establish context = () =>
    {
        dbContext = new ProductDbContext(ConnectionStringProvider.GetConnectionString(BusinessDomain.Product));
    };

    Because of = () =>
                     {
                         product = dbContext.Products.First(x => x.Id == 2688);
                     };

    // The next four tests should be false but all pass ( the actual count is 4 for the given record)
    It should_have_a_known_number_of_Classifications = () => product.Classifications.Count().Equals(9999);
    It should_have_a_known_number_of_Classifications2 = () => product.Classifications.Count().Equals(1);
    It should_have_a_known_number_of_Classifications3 = () => product.Classifications.Count().Equals(-99);
    It should_have_a_known_number_of_Classifications4 = () => product.Classifications.Count().Equals(4.5);
    //
}
Nunit中编写的相同测试正常工作

[Test]
public void It_should_have_a_known_number_of_Classifications()
{
    private ProductDbContext dbContext = new ProductDbContext(ConnectionStringProvider.GetConnectionString(BusinessDomain.Product));;
    Product product = dbContext.Products.Find(2688);
    // true
    Assert.That(product.Classifications.Count(), Is.EqualTo(4));

    // All of these will correctly regester as false
    //Assert.That(product.Classifications.Count(), Is.EqualTo(9999));
    //Assert.That(product.Classifications.Count(), Is.EqualTo(1));
    //Assert.That(product.Classifications.Count(), Is.EqualTo(-99));
    //Assert.That(product.Classifications.Count(), Is.EqualTo(4.5));
}

作为EF4和MSpec的新手,我希望有人能指出我的错误

您应该使用MSpec的断言库:

It should_have_a_known_number_of_Classifications =
  () => product.Classifications.Count().ShouldEqual(9999);

MSpec(和NUnit)根据框架方法中的代码(
[test]
It
)是否引发异常来识别测试结果(成功、失败)。您可以使用.NET Framework的
object.Equals()
方法来断言只返回true或false的值。因此,MSpec认为您的规范是成功的,因为
Equals()
没有引入不平等性。

这正是它的本质。谢谢