Mocking & Stubbing WCF methods using Rhino Mocks

367 views
Skip to first unread message

Tony

unread,
Jun 26, 2012, 3:36:01 PM6/26/12
to rhino...@googlegroups.com
Hi,

I have question regarding stubbing WCF methods. 
This example will show you what I am trying to do.

I'm coding in C#, VS 2008, Rhino Mocks

//WCF

[ServiceContract]
public interface IDataService
 {
       [OperationContract]
       string GetData();

       [OperationContract]
       string CallRealValue();

        [OperationContract]
       string CallStubValue();

}

public class DataService : IDataService
{
       public string GetData()
       {
            string result = CallRealValue();
       }
     
       public virtual string CallRealValue()
       {
               //doing something
               return "Real";
       }

       public virtual string CallStubValue()
       {
               //doing something
              return "Stub";
       }
}

//In my console project, I have reference to the WCF so that I can access their members [ServiceReferenceData]

public class Test
{
    public static void Main()
    {
           ServiceReferenceData.DataServiceClient dsc = MockRepository.GeneratePartialMock<ServiceReferenceData.DataServiceClient>();
       
           dsc.Expect(x => x.CallRealValue())
                 .WhenCalled(t => t.ReturnValue = dsc.CallStubValue())
                 .Return(string.Empty)


           string result = dsc.GetData();

           //After above statement 'result' variable should be 'Stub' instead of null. I could not figured it out whats wrong with my syntex. 
           //However, I tried the same thing with the simpler example that is not WCF service methods and whenever I call GetData() method,
           //inside of that method it always goes to CallStubValue() which was expected and I was getting the correct result back but for WCF 
           //service it does not working.

     }

}

Any help would be appreciated. 
Thanks,

Adam

unread,
Jun 26, 2012, 3:39:43 PM6/26/12
to rhino...@googlegroups.com
Doesn't it work if you take away ".Return(string.Empty)" at the end?

Tony

unread,
Jun 26, 2012, 3:56:07 PM6/26/12
to rhino...@googlegroups.com
Hello Adam, 

I tried without ".Return(string.Empty)" but it was giving me 'InvalidOperationException' because it needed the return value.

Adam

unread,
Jun 26, 2012, 4:06:17 PM6/26/12
to rhino...@googlegroups.com
It should then work if you change it to ".Return("Stub")" 
What you have is expecting that you make the call to CallRealValue(), which is called by GetData(), but you're telling it to Return(string.Empty) instead, which, I believe, overrides the t.ReturnValue = dsc.CallStubValue().

You should also be able to do dsc.Stub(x => x.CallRealValue()).IgnoreArguments().Return("Stub")  which will create a fake CallRealValue method that will return "Stub" regardless of the supplied Arguments -- the IgnoreArguments() part is optional, depending on what you want it to do.

So try one (or both) of those and let me know if either work.

Tony

unread,
Jun 26, 2012, 4:24:27 PM6/26/12
to rhino...@googlegroups.com
If I use  dsc.Stub(x => x.CallRealValue()).IgnoreArguments().Return("Stub") then its working if I am calling 'CallRealValue()' method but does not work if I am calling 'GetData()'. This is what I want because most of the time inside of 'GetData()' method after calling 'CallRealValue()' I would be using the return value (which i did not show bcz of simplicity).

Same thing applies to ".Return("Stub")". Its working but I do not just want the return value I also want what it supposed to do other things in the stub method. I my original application its return value comes from some other service which i did not showed here because of simplicity. 

I tried lot of different things for this part but couldn't figured it out but when I just had simple class with the console App it worked the first time. That is why I was confused why Rhino Mock is not behaving the same way.

Thanks,

Adam Van Aken

unread,
Jun 26, 2012, 4:38:45 PM6/26/12
to rhino...@googlegroups.com
Ohh I see what you mean now.  I'm sorry, I am out of ideas :\  I am new to Rhino.Mocs.
If I come across something I will let you know!

--Adam

--
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rhinomocks/-/RbyoNSJEitIJ.

To post to this group, send email to rhino...@googlegroups.com.
To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rhinomocks?hl=en.

Tony

unread,
Jun 26, 2012, 6:24:42 PM6/26/12
to rhino...@googlegroups.com
Thanks,
--Adam

To unsubscribe from this group, send email to rhinomocks+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages