Newbie, need help with simple mock for void method

166 views
Skip to first unread message

E Jo

unread,
Nov 6, 2012, 1:02:36 PM11/6/12
to rhino...@googlegroups.com
Can anyone help with a simple mock for void methods. I'm been trying all the suggested examples and still cannot the method to be mock correctly.

Below is the code example:

==================================

public class TimerControl : ITimerControl
{

Public void TimerMethod()
{
           Start();
}

}

==================================

I've tried the following and it won't work.

ITimerControl target = MockRepository.GenerateMock<ITimerControl>();

target.AssertWasNotCalled(method=>Method.TimerMethod());

============  this did not do nothing, it did not hit the TimerMethod.

And the tried the following also:

ITimerControl mock = MockRepository.GenerateMock<ITimerControl>();

Expect.Call( mock.TimerMethod);

=========== no success.

I'm new to Unit testing and tried very hard to find tutorials on Rhino Mock and read the documentation, but I'm still a noob in rhino mock and unit testing. Any help with this simple mock?

-EJ





Mark Wilkinson

unread,
Nov 6, 2012, 1:24:16 PM11/6/12
to rhino...@googlegroups.com
can you post the whole Unit Test class, the TestFixture you're using for this code below.


From: E Jo <savage...@gmail.com>
To: rhino...@googlegroups.com
Sent: Tuesday, November 6, 2012 12:02 PM
Subject: [RhinoMocks] Newbie, need help with simple mock for void method

--
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/-/AUuQUe42OO4J.
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.


E Jo

unread,
Nov 6, 2012, 2:59:12 PM11/6/12
to Rhino.Mocks
[TestClass]
public Class TimerControllerTest
{
[TestMethod]
public void TimerMethod_Test1()
{
ITimerControl target =
MockRepository.GenerateMock<ITimerControl>();

target.AssertWasNotCalled(method=>Method.TimerMethod());
}


[TestMethod]
public void TimerMethod_Test2()
{
ITimerControl mock =
MockRepository.GenerateMock<ITimerControl>();

Expect.Call( mock.TimerMethod);
}

}

On Nov 6, 11:24 am, Mark Wilkinson <marka...@yahoo.com> wrote:
> can you post the whole Unit Test class, the TestFixture you're using for this code below.
>
>
>
>
>
>
>
> >________________________________
> > From: E Jo <savagecomm...@gmail.com>
> >To: rhino...@googlegroups.com
> >Sent: Tuesday, November 6, 2012 12:02 PM
> >Subject: [RhinoMocks] Newbie, need help with simple mock for void method
>
> >Can anyone help with a simple mock for void methods. I'm been trying all the suggested examples and still cannot the method to be mock correctly.
>
> >Below is the code example:
>
> >==================================
>
> >public class TimerControl : ITimerControl
> >{
>
> >Public void TimerMethod()
> >{
> >           Start();
> >}
>
> >}
>
> >==================================
>
> >I've tried the following and it won't work.
>
> >ITimerControl target = MockRepository.GenerateMock<ITimerControl>();
>
> >target.AssertWasNotCalled(method=>Method.TimerMethod());
>
> >============  this did not do nothing, it did not hit the TimerMethod.
>
> >And the tried the following also:
>
> >ITimerControl mock = MockRepository.GenerateMock<ITimerControl>();
>
> >Expect.Call( mock.TimerMethod);
>
> >=========== no success.
>
> >I'm new to Unit testing and tried very hard to find tutorials on Rhino Mock and read the documentation, but I'm still a noob in rhino mock and unit testing. Any help with this simple mock?
>
> >-EJ
>
> >--
> >You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> >To view this discussion on the web visithttps://groups.google.com/d/msg/rhinomocks/-/AUuQUe42OO4J.

Mark Wilkinson

unread,
Nov 6, 2012, 3:15:48 PM11/6/12
to rhino...@googlegroups.com

Sorry here's my answer:



You're not testing anything.

If you're creating a mock then the system under test needs to use that mock.

You have no system under test inside this TestClass.

A system under test would be the consumer of this interface.

in this case you'd use it like:

myClass.SomeMethod(); // in SomeMethod you expect TimerMethod to be called or not to be called, based on the logic inside that method.

then your next line would be:

target.AssertWasNotCalled(method=>Method.TimerMethod());


From: E Jo <savage...@gmail.com>
To: Mark Wilkinson <mark...@yahoo.com>
Sent: Tuesday, November 6, 2012 12:33 PM
Subject: Re: Newbie, need help with simple mock for void method

[TestClass]
public Class TimerControllerTest
{
      [TestMethod]
      public void TimerMethod_Test1()
      {
        ITimerControl target =
MockRepository.GenerateMock<ITimerControl>();

        target.AssertWasNotCalled(method=>Method.TimerMethod());
      }


      [TestMethod]
      public void TimerMethod_Test2()
      {

          ITimerControl mock =
MockRepository.GenerateMock<ITimerControl>();

          Expect.Call( mock.TimerMethod);
      }

}





On Nov 6, 11:24 am, Mark Wilkinson <marka...@yahoo.com> wrote:
> can you post the whole Unit Test class, the TestFixture you're using for this code below.
>
>
>
>
>
>
>
> >________________________________
> > From: E Jo <savagecomm...@gmail.com>

> >To: rhino...@googlegroups.com
> >Sent: Tuesday, November 6, 2012 12:02 PM
> >Subject: [RhinoMocks] Newbie, need help with simple mock for void method
>
> >Can anyone help with a simple mock for void methods. I'm been trying all the suggested examples and still cannot the method to be mock correctly.
>
> >Below is the code example:
>
> >==================================
>
> >public class TimerControl : ITimerControl
> >{
>
> >Public void TimerMethod()
> >{
> >           Start();
> >}
>
> >}
>
> >==================================
>
> >I've tried the following and it won't work.
>
> >ITimerControl target = MockRepository.GenerateMock<ITimerControl>();
>
> >target.AssertWasNotCalled(method=>Method.TimerMethod());
>
> >============  this did not do nothing, it did not hit the TimerMethod.
>
> >And the tried the following also:
>
> >ITimerControl mock = MockRepository.GenerateMock<ITimerControl>();
>
> >Expect.Call( mock.TimerMethod);
>
> >=========== no success.
>
> >I'm new to Unit testing and tried very hard to find tutorials on Rhino Mock and read the documentation, but I'm still a noob in rhino mock and unit testing. Any help with this simple mock?
>
> >-EJ
>
> >--
> >You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> >To view this discussion on the web visithttps://groups.google.com/d/msg/rhinomocks/-/AUuQUe42OO4J.

> >To post to this group, send email to rhino...@googlegroups.com.
> >To unsubscribe from this group, send email to rhinomocks+unsub...@googlegroups.com.

E Jo

unread,
Nov 6, 2012, 3:30:16 PM11/6/12
to Rhino.Mocks
System - as in the Namespace "using System.IO" or the class.

The code below is the class I'm trying to test:

======================
public class TimerControl : ITimerControl
{
Public TimerControl()
{
}

Public void TimerMethod()
{
Start();
}
}

======================

I just want to know if TimerMethod() was fired.

I've seen various ways of checking the void method within this Google
Group, but they have layers of layers of code, my code is just simple.
It has a empty constructor (which will be filled later) and void
methods for different flows. My TimerControl Class just has void
methods w/out parameters. I just want to test the flow, in which i
want to know if the TimerMethod was executed.



On Nov 6, 1:15 pm, Mark Wilkinson <marka...@yahoo.com> wrote:
> Sorry here's my answer:
>
>
>
>
>
>
>
>
>
>
>
> >You're not testing anything.
>
> >If you're creating a mock then the system under test needs to use that mock.
>
> >You have no system under test inside this TestClass.
>
> >A system under test would be the consumer of this interface.
>
> >in this case you'd use it like:
>
> >myClass.SomeMethod(); // in SomeMethod you expect TimerMethod to be called or not to be called, based on the logic inside that method.
>
> >then your next line would be:
>
> >target.AssertWasNotCalled(method=>Method.TimerMethod());
>
> >>________________________________
> >> From: E Jo <savagecomm...@gmail.com>
> >>To: Mark Wilkinson <marka...@yahoo.com>
> >>> >To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.

Mark Wilkinson

unread,
Nov 6, 2012, 3:36:37 PM11/6/12
to rhino...@googlegroups.com
so you want to find out if Start() is called?


From: E Jo <savage...@gmail.com>
To: Rhino.Mocks <rhino...@googlegroups.com>
Sent: Tuesday, November 6, 2012 2:30 PM
Subject: [RhinoMocks] Re: Fw: Newbie, need help with simple mock for void method
> >>> >To unsubscribe from this group, send email to rhinomocks+unsub...@googlegroups.com.

> >>> >For more options, visit this group athttp://groups.google.com/group/rhinomocks?hl=en.

--
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to rhino...@googlegroups.com.
To unsubscribe from this group, send email to rhinomocks+unsub...@googlegroups.com.

E Jo

unread,
Nov 6, 2012, 3:38:48 PM11/6/12
to Rhino.Mocks
yes, i would like to know if Start() was called inside the
TimerMethod().



On Nov 6, 1:36 pm, Mark Wilkinson <marka...@yahoo.com> wrote:
> so you want to find out if Start() is called?
>
>
>
>
>
>
>
> >________________________________
> > From: E Jo <savagecomm...@gmail.com>
> >> >>> >To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.
> >> >>> >For more options, visit this group athttp://groups.google.com/group/rhinomocks?hl=en.
>
> >--
> >You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> >To post to this group, send email to rhino...@googlegroups.com.
> >To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.

Mark Wilkinson

unread,
Nov 6, 2012, 3:55:20 PM11/6/12
to rhino...@googlegroups.com
What is Start() a part of? What class?  I assume the Timer class?

you need to then have your ITimerControl consumed by some other class and you would assert that your method "TimerMethod()" is called when expected.

I think I understand what you're trying to do, to prevent actually calling a real timer to start, so you're on the right track with the purpose of Mocking but some other class needs to be your system under test, the consumer of the ITimerControl class.


From: E Jo <savage...@gmail.com>
To: Rhino.Mocks <rhino...@googlegroups.com>
Sent: Tuesday, November 6, 2012 2:38 PM
> >> >>> >To unsubscribe from this group, send email to rhinomocks+unsub...@googlegroups.com.

> >> >>> >For more options, visit this group athttp://groups.google.com/group/rhinomocks?hl=en.
>
> >--
> >You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> >To post to this group, send email to rhino...@googlegroups.com.
> >To unsubscribe from this group, send email to rhinomocks+unsub...@googlegroups.com.

> >For more options, visit this group athttp://groups.google.com/group/rhinomocks?hl=en.

--
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to rhino...@googlegroups.com.
To unsubscribe from this group, send email to rhinomocks+unsub...@googlegroups.com.

E Jo

unread,
Nov 6, 2012, 5:02:34 PM11/6/12
to Rhino.Mocks
here the summary of the TimerControl class, sorry I was trying to keep
it simple to just the method. Start is apart of the TimerControl
class.

======================
public class TimerControl : ITimerControl
{
Public TimerControl()
{
}

Public void TimerMethod()
{
Start();
}

Public void PauseTimer()
{
\\Loops thru a set of list threads
}

Public void ContinueTimer()
{
Start();
}

Public void Start()
{
\\ foreach loop thru a list
}

Public void Stop()
{
PauseTimers();
}

}

======================

I've tried the following.

[TestMethod]
public void ContinueTimer_Test()
{
var mockTimer = MockRepository.GenerateMock<ITimerControl>();
mockTimer.ContinueTimer();

mockTimer.VerifyAllExpectations();
}

I was going by this suggestion from this site
http://www.ronaldwidha.net/2009/01/30/mocking-void-method-with-rhino-mocks/

but I get his error " Invalid call, the last call has been used or no
call has been made (make sure that you are calling a virtual (C#) /
Overridable (VB) method)."


On Nov 6, 1:55 pm, Mark Wilkinson <marka...@yahoo.com> wrote:
> What is Start() a part of? What class?  I assume the Timer class?
>
> you need to then have your ITimerControl consumed by some other class and you would assert that your method "TimerMethod()" is called when expected.
>
> I think I understand what you're trying to do, to prevent actually calling a real timer to start, so you're on the right track with the purpose of Mocking but some other class needs to be your system under test, the consumer of the ITimerControl class.
>
>
>
>
>
>
>
> >________________________________
> >> >> >>> >To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.
> >> >> >>> >For more options, visit this group athttp://groups.google.com/group/rhinomocks?hl=en.
>
> >> >--
> >> >You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> >> >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 athttp://groups.google.com/group/rhinomocks?hl=en.
>
> >--
> >You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> >To post to this group, send email to rhino...@googlegroups.com.
> >To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.

Mark Wilkinson

unread,
Nov 6, 2012, 5:14:23 PM11/6/12
to rhino...@googlegroups.com
yeah I think there is still a point of mocking you're not getting.

The TimerControl class is your mock, so it needs to be consumed by another class.

You don't need to test if a mock calls itself.

Lets say you have a Controller:

public class MyController : Controller
{
       private readonly ITimerControl _timer;

public MyController(ITimerControl timer)
        {
              _timer = timer;
        } 

void ButtonStart()
       {
    // some logic that might determine whether timer.TimerMethod() is called
       }

}

// ignore the NUnit attributes, use microsoft's for your case
[TestFixture]
public class ControllerTests
{
       // System under Test
       private MyController _controllerSUT;
     
       // Dependencies
private ITimerControl _timerControl;
      
       [SetUp]
       public void Setup()
       {  
           _timerControl = MockRepository.GenerateMock<ITimerControl>();
           _controllerSUT = new MyController(_timerControl);    
       }

       [Test]
       public void ButtonStart_should_call_timerControl_TimerMethod()
       {
             _controllerSUT.ButtonStart();
    // you're asserting that ButtonStart when invoked should call the dependency method
           // this could be AssertWasNotCalled, depends on the logic you're testing
             _timerControl.AssertWasCalled(d => d.TimerMethod());
       }
}


From: E Jo <savage...@gmail.com>
To: Rhino.Mocks <rhino...@googlegroups.com>
Sent: Tuesday, November 6, 2012 4:02 PM
> >> >> >>> >To unsubscribe from this group, send email to rhinomocks+unsub...@googlegroups.com.

> >> >> >>> >For more options, visit this group athttp://groups.google.com/group/rhinomocks?hl=en.
>
> >> >--
> >> >You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> >> >To post to this group, send email to rhino...@googlegroups.com.
> >> >To unsubscribe from this group, send email to rhinomocks+unsub...@googlegroups.com.

> >> >For more options, visit this group athttp://groups.google.com/group/rhinomocks?hl=en.
>
> >--
> >You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> >To post to this group, send email to rhino...@googlegroups.com.
> >To unsubscribe from this group, send email to rhinomocks+unsub...@googlegroups.com.

> >For more options, visit this group athttp://groups.google.com/group/rhinomocks?hl=en.

--
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to rhino...@googlegroups.com.
To unsubscribe from this group, send email to rhinomocks+unsub...@googlegroups.com.

E Jo

unread,
Nov 6, 2012, 5:26:49 PM11/6/12
to Rhino.Mocks
thanks, that helped.

followed your code and it work, thanks.
> > From: E Jo <savagecomm...@gmail.com>
> >To: Rhino.Mocks <rhino...@googlegroups.com>
> >http://www.ronaldwidha.net/2009/01/30/mocking-void-method-with-rhino-...
> >> >> >> >>> >To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.
> >> >> >> >>> >For more options, visit this group athttp://groups.google.com/group/rhinomocks?hl=en.
>
> >> >> >--
> >> >> >You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> >> >> >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 athttp://groups.google.com/group/rhinomocks?hl=en.
>
> >> >--
> >> >You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> >> >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
>
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages