JsMockito - 如何让jsmockito忽略参数

时间:2013-04-15 06:39:25

标签: javascript mocking

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

假设我有以下代码: -

var mockedCommand = mock(CoreCommand);

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

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

感谢。

1 个答案:

答案 0 :(得分: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");

});