1

いくつかの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の両方にかなり慣れていないので、誰かが私のエラーを指摘できることを望んでいます。

4

1 に答える 1

2

MSpecのアサーションライブラリを使用する必要があります。

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

[Test]MSpec(およびNUnit)は、フレームワークのメソッド( 、It)内のコードが例外をスローするかどうかに基づいて、テスト結果(成功、失敗)を識別します。.NET Frameworkのobject.Equals()メソッドを使用して、trueまたはfalseを返すだけのアサートを行います。Equals()したがって、MSpecは、不平等を引き起こさないため、仕様は成功したと見なします。

于 2012-06-26T12:13:38.143 に答える