Reputation: 7666
I am trying to set up a unit test for a piece of code that uses a spelling corrector. I have the code properly dependency injected, so setting up the stub in Rhinomocks is not a problem, but the block of text I've created for the test is a good 50 words long and I would really rather not have 50 lines of code that look something like this:
spellingCorrector.Stub(x => x.CorrectWord("the")).Return("the");
spellingCorrector.Stub(x => x.CorrectWord("boy")).Return("boy");
spellingCorrector.Stub(x => x.CorrectWord("ran")).Return("ran");
For the purposes of my unit tests I think assuming that the words are spelled right is okay. Is there a way to get Rhinomocks to simply follow a rule about returning, something to the effect of:
spellingCorrector.Stub(x => x.CorrectWord(y)).Return(y);
Upvotes: 2
Views: 62
Reputation: 6679
If you're not particularly attached to Rhinomock, you can use Moq:
spellingCorrector.Setup(x => x.CorrectWord(It.IsAny<string>()))
.Returns(x => x);
Upvotes: 0
Reputation: 17657
For anything complex like this, instead of using RhinoMocks, write your own little stub class. I'd back it with a Dictionary of all the words that should be corrected, and return the word if it isn't in the Dictionary.
Mocks were only created to make this kind of thing easier. If it isn't easier (or more importantly, more readable) to use mocks, just write the code.
Upvotes: 0
Reputation: 1039238
You could use the IgnoreArguments()
method:
spellingCorrector
.Stub(x => x.CorrectWord(null))
.IgnoreArguments()
.Return(y);
This way no matter what value is passed to the CorrectWord
method, it will return y
.
UPDATE:
After your comment it is more clear:
Func<string, string> captureArg = arg => arg;
spellingCorrector.Stub(x => x.CorrectWord(null)).IgnoreArguments().Do(captureArg);
This will use whatever value is passed as argument as return value. Adapt the captureArg
delegate if you need to perform some transformations on this return value.
Upvotes: 3