Should be very basic...

5 posts, 0 answers
  1. Matthew
    Matthew avatar
    16 posts
    Member since:
    Jun 2014

    Posted 07 Aug 2014 Link to this post

    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.
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 11 Aug 2014 Link to this post

    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.

     
  3. DevCraft R3 2016 release webinar banner
  4. Matthew
    Matthew avatar
    16 posts
    Member since:
    Jun 2014

    Posted 11 Aug 2014 in reply to Kaloyan Link to this post

    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.
  5. Matthew
    Matthew avatar
    16 posts
    Member since:
    Jun 2014

    Posted 11 Aug 2014 in reply to Matthew Link to this post

    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.
  6. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 14 Aug 2014 Link to this post

    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.

     
Back to Top
DevCraft R3 2016 release webinar banner