{"action":"created","issue":{"url":"https://api.github.com/repos/golang/mock/issues/191","repository_url":"https://api.github.com/repos/golang/mock","labels_url":"https://api.github.com/repos/golang/mock/issues/191/labels{/name}","comments_url":"https://api.github.com/repos/golang/mock/issues/191/comments","events_url":"https://api.github.com/repos/golang/mock/issues/191/events","html_url":"https://github.com/golang/mock/pull/191","id":326285732,"number":191,"title":"Define fallback for mock methods","user":{"login":"ybbus","id":1547663,"avatar_url":"https://avatars1.githubusercontent.com/u/1547663?v=4","gravatar_id":"","url":"https://api.github.com/users/ybbus","html_url":"https://github.com/ybbus","followers_url":"https://api.github.com/users/ybbus/followers","following_url":"https://api.github.com/users/ybbus/following{/other_user}","gists_url":"https://api.github.com/users/ybbus/gists{/gist_id}","starred_url":"https://api.github.com/users/ybbus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ybbus/subscriptions","organizations_url":"https://api.github.com/users/ybbus/orgs","repos_url":"https://api.github.com/users/ybbus/repos","events_url":"https://api.github.com/users/ybbus/events{/privacy}","received_events_url":"https://api.github.com/users/ybbus/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-05-24T20:43:22Z","updated_at":"2018-05-24T21:51:53Z","closed_at":null,"author_association":"NONE","pull_request":{"url":"https://api.github.com/repos/golang/mock/pulls/191","html_url":"https://github.com/golang/mock/pull/191","diff_url":"https://github.com/golang/mock/pull/191.diff","patch_url":"https://github.com/golang/mock/pull/191.patch"},"body":"Hi.\r\n\r\nI would appreciate your feedback for this pull request.\r\n\r\nIt is kind of similar to: https://github.com/golang/mock/pull/188\r\n\r\nFor each method you can define a fallback / default behaviour by calling:\r\n\r\nmockObject.EXPECT().FooMethod().Fallback().Returns(5)\r\n\r\nAfter defining this expectation as fallback, if there is no other expectation that matches a call to this method, the above defined fallback will be executed and return 5 all the time.\r\n\r\nThis is very helpful to define a default behaviour for methods. This makes more sense than loose mocks since most of the time the default return value is not very handy (e.g. nil, \"\" etc..). \r\n\r\nThe following rules for Fallback() should be noted (see the :gomock/controller_test.go for examples)\r\n- all expectations are tried to match before the fallback method is considered\r\n- the parameters of a method expectation in a fallback are not matched, this would not make sense, so you can leave the parameters empty or use arbitrary ones\r\n- MinTimes, MaxTimes and AnyTime have no effect on a fallback method\r\n- calling After() on a fallback will throw an error\r\n- using a fallback as prerequisite will throw an error\r\n- a fallback of a method is overwritten when another fallback of the same method is defined later on\r\n- of course still all concrete expectations must be met, otherwise Finish() will fail as usual\r\n\r\nI tried to change as little as possible in the code for this feature to work."},"comment":{"url":"https://api.github.com/repos/golang/mock/issues/comments/391876970","html_url":"https://github.com/golang/mock/pull/191#issuecomment-391876970","issue_url":"https://api.github.com/repos/golang/mock/issues/191","id":391876970,"user":{"login":"ybbus","id":1547663,"avatar_url":"https://avatars1.githubusercontent.com/u/1547663?v=4","gravatar_id":"","url":"https://api.github.com/users/ybbus","html_url":"https://github.com/ybbus","followers_url":"https://api.github.com/users/ybbus/followers","following_url":"https://api.github.com/users/ybbus/following{/other_user}","gists_url":"https://api.github.com/users/ybbus/gists{/gist_id}","starred_url":"https://api.github.com/users/ybbus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ybbus/subscriptions","organizations_url":"https://api.github.com/users/ybbus/orgs","repos_url":"https://api.github.com/users/ybbus/repos","events_url":"https://api.github.com/users/ybbus/events{/privacy}","received_events_url":"https://api.github.com/users/ybbus/received_events","type":"User","site_admin":false},"created_at":"2018-05-24T21:51:53Z","updated_at":"2018-05-24T21:51:53Z","author_association":"NONE","body":"I thought of \"Fallback\" because it tells what it is: a Fallback :)\r\nIt should be called if no expectation that was defined matches the method call.\r\nBut I am fine with something like \"Default\".\r\n\r\nI think a Fallback should be something very simple that does stupidly return some value.\r\nIf you allow to use matchers you will end up with a lot of defaults / fallbacks. For me this does not sound like a \"fallback\" anymore. If you have the need for different defaults I think you should consider providing them as actual expectations.\r\n\r\nI thought of fallback as something like: \"I need to mock this but I do not really care what the consumer of this mock does with it. Just mock it and return some static value\".\r\n\r\nBut indeed it would be maybe more clean to consider matchers.\r\nThe catch all case for a defualt then could be:\r\nmockObject.EXPECT().FooMethod(Any()).WillByDefault().Return(5)\r\n\r\nLooks reasonable\r\nI will try to integrate this.\r\nI also like the WillByDefault method name."}}
{"actor":{"display_login":"ybbus"}}