質問

NUnit と NMock2 を使用すると、同じ SqlParameters だと思っていたものを比較できませんでした。

SqlParameter param1 = new SqlParameter("@Id", 1);
SqlParameter param2 = new SqlParameter("@Id", 1);
Assert.IsTrue(param1.Equals(param2));  // This failed

NMock2 を使用してメソッドの実行をテストしようとしたときに、この問題に遭遇しました。

[Test]
    public void UpdateComments()
    {
        const int arbitraryId = 1;
        Comment comment = new Comment();

        SqlParameter idParam = new SqlParameter("@ChangeId", arbitraryId);

        Expect.Once.On(mockSqlDao).Method("ExecuteNonQuery")
              .With("usp_Update_Comment", idParam);

        changeDao.UpdateComment(arbitraryId, comment);

        mocks.VerifyAllExpectationsHaveBeenMet();
    }

次のエラーを受け取りました:

NMock2.Internal.ExpectationException:sqldao.executenonqueryの予期しない呼び出し( "usp_update_comment"、)予想:1回:sqlDao.ExecuteNonQuery(「usp_Update_Comment」に等しい、<@ChangeId>に等しい) [0 回呼び出されました]

質問:

  • パラメーターがSQLParameterであると予想した場合、NMOCK2でどのようにテストしますか?
  • 2 つの SqlParameter の同等性をどのように比較しますか?
役に立ちましたか?

解決

私の知る限り、 .Equals() は Equals のデフォルト実装を使用しているため (これは、SqlParameter が別の SqlParameter が同じオブジェクトである場合にのみ「等しい」ことを意味します)、パラメータのプロパティを直接問い合わせる必要があります。正しいデータが渡されていることを確認します。

.With 内の Has.Property 呼び出しを使用すると、パラメーターが他の値と等しい必要なく、パラメーターのプロパティをチェックできます。次のことを試してください。

Expect.Once.On(mockSqlDao).Method("ExecuteNonQuery")
          .With("usp_Update_Comment", Has.Property("ParameterName").EqualTo("@Id") & 
                                      Has.Property("Value").EqualTo(1));
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top