开发者

Argument constraints in RhinoMock methods

开发者 https://www.devze.com 2022-12-23 17:09 出处:网络
I am mocking a repository that should have 1 entity in it for the test scenario. The repository has to return this entity based on a known id and return nothing when other ids are passed in.

I am mocking a repository that should have 1 entity in it for the test scenario. The repository has to return this entity based on a known id and return nothing when other ids are passed in.

I have tried doing something like this:

开发者_运维问答_myRepository.Expect(item => item.Find(knownId)).Return(knownEntity);
_myRepository.Expect(item => item.Find(Arg<Guid>.Is.Anything)).Return(null);

It seems however the second line is overriding the first and the repository always returns null. I don't want to mock all the different possible IDs asked (they could go up to hundreds) when the test scenario is only concerned with the value of one Id.

I understand that I can change the second line to be the negative of the first. However this would become more and more difficult (as well as ugly) if the repository has to return more than 1 entity for a test scenario.


It did work for me correctly. My guess is that you are searching for your known id more than once. Try changing the quoted lines to:

_myRepository.Stub(item => item.Find(knownId)).Return(knownEntity);
_myRepository.Stub(item => item.Find(Arg<Guid>.Is.Anything)).Return(null);

and let me know if it solves the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号