2

現在、コントローラーが使用するモックされたメソッドのテストで問題が発生しています。特定の列挙型の戻り値の型があります。現在、このモックされたメソッドから、返すように指定した値ではなく、デフォルトの列挙値を常に取得しています。何か不足していますか?Moq と JustMock lite の両方を試してみましたが、同じ結果が得られました。以下の JustMock lite の例。

コードをコピーする際に間違いがなかったことを願っています。オブジェクトの名前をすべて変更したので、申し訳ありません。

単体テストの一部を次に示します。

        var returnStatus = ExampleEnum.Invalid;

        //Mock the client
        var client = Mock.Create<ITestInterface>();

        Mock.Arrange(() => client.ValidateSomething(Guid.NewGuid()))
            .Returns(returnStatus).MustBeCalled();


        var testController = new TestController(client);

        var result = testController.DoSomething(Guid.NewGuid().ToString()) as ViewResult;

コントローラーからの関連ビットは次のとおりです。

        private ITestInterface _client { get; set; }

        public TestController(ITestInterface client)
        {
            _client = client;
        }

これが私のコントローラーアクションの一部です:

 public ActionResult DoSomething(string id)
        {
            Guid token;
            if(!string.IsNullOrEmpty(id) && Guid.TryParse(id, out token))
            {
                using (var client = _client)
                {
                    ApplicationUser applicationUser;
                    var status = client.ValidateSomething(token);

                    switch (status)
                    {

クライアントは正しくモックされますが、返される「ステータス」プロパティは常に ExampleEnum.DefaultValue であり、結果として指定した値ではありません。

十分な情報を提供できたことを願っています。どんな助けでも大歓迎です。

4

1 に答える 1

0

You probably did your setup wrong.

Guid.NewGuid() returns a new random GUID, so the GUID you use to setup your mock and the GUID you use to call the DoSomething method will never be the same.

You should do something like:

var guid = Guid.NewGuid()
...
Mock.Arrange(() => client.ValidateSomething(guid))
        .Returns(returnStatus).MustBeCalled();
...
var result = testController.DoSomething(guid.ToString()) as ViewResult;

using the same GUID for the mock and for the call to DoSomething.


I don't know about JustMock, but with Moq you could also simply use It.IsAny to match all GUIDs:

client.Setup(c => c.ValidateSomething(It.IsAny<Guid>())).Returns(returnStatus);
于 2014-06-25T14:33:07.300 に答える