I'm trying to write some Mspec tests against some EF4 objects. However they are returning false positives. I don't know if it is the way that I've written the tests or if there is something else going on. The test class is below:

[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);
    //
}

The same tests written within Nunit work correctly.

[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));
}

Being fairly new to both EF4 and MSpec, I'm hoping someone can point out my error.

有帮助吗?

解决方案

You should use MSpec's assertion library:

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

MSpec (and NUnit) identify test outcomes (successful, failing) based on if the code inside the framework's methods ([Test], It) throws an exception. You use the .NET Framework's object.Equals() method to assert which just returns true or false. Therefore, MSpec considers your specifications as successful, because Equals() doesn't throw on inequality.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top