Are you married? Yeah, quite not usual question for blog post about mocking, but if you are married you probably asked your girl to become your wife.
So you are playing role of PotentialHasband:
publicinterfaceIPotentialHusband
{
string
AskForHerHandInMarriage();
}
and she is PotentialWife, taking your ask into consideration and answering you true or false.
publicinterfaceIPotentialWife
{
bool
GiveHimAnswer(string hisSpeech);
}
If she is Linda, you would need to have “I love you” in your speech to get her agreement:
publicclassLinda : IPotentialWife
{
public bool GiveHimAnswer(string hisSpeech)
{
return hisSpeech.Contains(“I love you”);
}
}
Btw, if your wife is indeed Linda, you just need put some comments below :)
What we are going to test is to see that after you asked, she will definitely give you at least some answer and not ignore you. Also if her answer is “YES” we would like to see MarriageService called Marry method.
Console.WriteLine(“You are now husband and wife!”);
}
}
Class which we are going to test is named LearningMocking and has constructor that accepts potential husband/wife and marriageService. Whole story takes place in RestaurantDinner method.
First let’s test that after you asked she gives at least some answer. Since we are not interested in concrete instances we just generate mocks for our interfaces, and passing null instead of marriage service. I’m using RhinoMocks in this example.
[Test]
publicvoid EnsureThatAfterHeAskedSheAnswers()
{
var potentialHusband = MockRepository.GenerateMock<IPotentialHusband>();
var
potentialWife = MockRepository.GenerateMock<IPotentialWife>();
var
learningRhino = newLearningMocking(potentialHusband,
potentialWife, null);
This ensured that at least she will give you some answer. In next test we will have some mix and will use concrete class Linda. We have generated Stub for return value of your speech, so instead of
writing concrete class for husband we’ve used stub. This test shows that
she will not marry you:
[Test]
publicvoid EnsureLindaWillRejectHim()
{
var potentialHusband = MockRepository.GenerateMock<IPotentialHusband>();
var
linda = newLinda();
var marriageService = MockRepository.GenerateStub<MarriageService>();
var
learningRhino = newLearningMocking(potentialHusband,
linda, marriageService);
Except of verifying that Marry method was called we also verify that it was called with instance of linda and none else with line Arg<IPotentialWife>.Is.Equal(linda). As well you could setup constructor arguments for MarriageService, since it is not an interface. Also with Rhino.Mocks you will need to have method Marry to be virtual. If it is not virtual and you don’t have interface for it you may need to extend that class with your own.
This website uses cookies. We'll assume you're ok with this, but you can opt-out if you wish.AcceptRead More
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
code
more code
~~~~