This is a migrated thread and some comments may be shown as answers.

Should be very basic...

4 Answers 66 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 07 Aug 2014, 07:07 PM
I figure this out... but I know it won't be very complicated.
I am calling this method and I want to mock the string "alpha" 

public void Request()
{
     if (string.isNullOrEmpty(alpha))
      {
           Do somthing bad
      }
     else
     {
          Do somthing good
      }

Pretty much I want alpha to be mocked so that it passes the if statement and goes on to do something good.

4 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 11 Aug 2014, 12:20 PM
Hello Matthew,

Thank you for contacting.

Normally, you should be able to mock the return value of alpha as long as it is not a field parameter. However, even then, with JustMock you can directly mock the string.IsNullOrEmpty() function to return either true or false.

I have prepared a sample solution for you, that contains two test methods for each of the scenarios. You will find it attached to this reply.

In general, if alpha is field parameter you can use the following:
Mock.Arrange(() => string.IsNullOrEmpty(Arg.AnyString)).Returns(false);
and if it is a property:
Mock.Arrange(() => mockedClass.AlphaProp).Returns("some non-empty string");

I hope this helps. Please, let me know if I can be of further assistance.

Regards,
Kaloyan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Matthew
Top achievements
Rank 1
answered on 11 Aug 2014, 12:38 PM
I have mocked this type of thing before but the way that I used did not work this time. Your solution did though, so thank you.

I think the hardest thing about using the Just Mock software is knowing when to use certain types of mocks and when to use others.
0
Matthew
Top achievements
Rank 1
answered on 11 Aug 2014, 02:37 PM
for the second option does the class have to be mocked or can it just be called in to the test?

Also to assign a value to the property of alpha would you use AlphaProp? I tried to use your second example and the it wont compile.
0
Kaloyan
Telerik team
answered on 14 Aug 2014, 09:36 AM
Hi Matthew,

Thank you for reaching back.

In the sample I provided, I am not using a mock of the Foo class. Instead, you should see that I am instantiating the actual type, like so:
// Arrange
var mock = new Foo();

Then, the arrange of AlphaProp still works, thanks to the JustMock's partial mocking feature. So, the answer to your first question is no, the class does not need to be mocked.

As for the second question, I am not sure if I understand you correctly. In my sample, I am mocking AlphaProp as this is the name of the property in that case. I use alpha as a parameter name in the Request function. If in your code, you make alpha as a property, there should be no problem to arrange its behavior. Please, excuse me if there is something that I am missing on this matter.

I hope this helps.

Regards,
Kaloyan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
General Discussions
Asked by
Matthew
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Matthew
Top achievements
Rank 1
Share this question
or