0

我想让 jsMockito 忽略我在函数调用中的参数输入。

可以说我有以下代码:-

var mockedCommand = mock(CoreCommand);

when(mockedCommand).parseCommand(myfakeInput, myfakeInput2, myFakeInput3).thenReturn("Hello");

我如何让 jsMockito 忽略 myFakeInput、myFakeInput2 和 myFakeInput3 并只返回“Hello”?

谢谢。

4

1 回答 1

1

我设法使用 JsHamcrest 对象匹配器解决了我的问题。

    var Employee = function()
    { 
        this.getName = function(name)
        {
            return "Hello " + name;
        }

        this.updateAddress = function(emp)
        {
            return emp; 
        }
    }; 

    test( "a test", function() {

      var mockObject = mock(Employee);

      var fakeAddress = {
        address1 : "Addr1", 
        address2 : "Addr2", 
        address3 : "Addr3", 
      };


    var nonMatchingAddress = {
        address1 : "DifferentAddr1", 
        address2 : "DifferentAddr2", 
        address3 : "DifferentAddr3", 
      };        

      var realAddress = {
        address1 : "123", 
        address2 : "456", 
        address3 : "789", 
      };

      // Ok 
      //when(mockObject).updateAddress(fakeAddress).thenReturn(realAddress);
      //var argumentMatchingReturnResult = mockObject.updateAddress(fakeAddress);
      //console.log(argumentMatchingReturnResult);
      //equal(argumentMatchingReturnResult, realAddress, "passing in matching argument.");

      // Not OK - return undefined 
      //when(mockObject).updateAddress(fakeAddress).thenReturn(realAddress);
      //var argumentMatchingReturnResult = mockObject.updateAddress(nonMatchingAddress);
      //console.log(argumentMatchingReturnResult); // comes out undefined 
      //equal(argumentMatchingReturnResult, realAddress, "passing in matching argument.");

      // uses JsHamcrest.Matchers.object() here 
      // match any object here 
      // when(mockObject).updateAddress(JsHamcrest.Matchers.object()).thenReturn(realAddress);
      // var result = mockObject.updateAddress(realAddress);
      //console.log(result);

      //notEqual(result, undefined, "ensure return result is not undefined");

      // uses JsHamcrest.Matchers.typeOf() here 
       when(mockObject).updateAddress(JsHamcrest.Matchers.typeOf('object')).thenReturn(realAddress);
       var result = mockObject.updateAddress(realAddress);
       console.log(result);
       notEqual(result, undefined, "ensure return result is not undefined");

});
于 2013-04-16T00:50:56.183 回答