Samah,
The code you want is below (I encourage you to paste into your code and run
(note the use of xUnit)...a few notes however
- I've used AAA syntax, which is our preferred method (over
record/replay)
- Since we're "stubbing" foo1 (that is intercepting calls made to it), it
needs to be virtual. Method foo2 does not have this requirement.
- I'm using xUnit as the testing framework - this should translate very
easily to MSTest (as it appears you are using)
- The correct output of this program should be "I'm in foo2" - even
though "I'm in foo1" coded into foo1, the call to it is intercepted.
Certainly if you have more questions, just ask.
Cheers,
Tim
public class FieldProblem_Samah
{
[Fact]
public void CanMockMethodWithEnvironmentPermissions()
{
// Arrange
var mockedFoo = MockRepository.GeneratePartialMock<fooClass>();
mockedFoo.Stub(x => x.foo1());
// Act
mockedFoo.methodToTest();
// Assert
mockedFoo.AssertWasCalled(x=>x.foo1());
}
}
public class fooClass
{
public void methodToTest ()
{
foo1();
foo2();
}
public virtual void foo1()
{
Console.WriteLine("I'm in foo1");
}
public void foo2()
{
Console.WriteLine("I'm in foo2");
}
}
On Wed, Aug 18, 2010 at 2:23 PM, samah <[email protected]> wrote:
>
> Thanks for your replyes..
>
> The senario I have is
>
> public class fooClass {
>
> public void methodToTest {
>
> ....
> calls foo1
> calls foo2
> }
>
> }
>
>
> I want to test if the "methodToTest" calls foo2 but I don't want it to
> break when calling foo1 and I want to use Partial Mocks because I want
> the test to use the actual methods when not set in the expectations
>
> [testMethod()]
> public void test()
> {
> MockRepository mocks = new MockRepository();
>
> fooClass fakeClass = mocks.PartialMock<fooClass>();
>
> ** fakeClass.Stub(x => x.foo1()).whenCalled(x => ;);
>
> using(mocks.Record())
> {
> fakeClass.foo2();
> }
>
> mocks.Verify(fakeClass);
>
> }
>
> How do I Stub foo1 on line *, I don't want to assert anything against
> it I just don't want to break my test becuase it is talking to an
> outsource dependency?
>
> Thanks,
>
> Samah
>
>
> On Aug 17, 9:11 pm, Tim Barcz <[email protected]> wrote:
> > Per my original email, I would actually suggest using a mock
> > MockRepository.GenerateMock<IFoo>();
> >
> > While you *can* assert on Stubs, it's muddied the waters a bit and the
> more
> > semantically correct use is a Mock.
> >
> > Tim
> >
> >
> >
> >
> >
> > On Tue, Aug 17, 2010 at 8:16 PM, David Tchepak <[email protected]>
> wrote:
> > > public interface IFoo {
> > > void DoFoo();
> > > }
> >
> > > var foo = MockRepository.GenerateStub<IFoo>();
> >
> > > //Do something when void method is called:
> > > foo.Stub(x => x.DoFoo()).WhenCalled(x => RunSomeCode());
> >
> > > //Assert void method was called
> > > foo.AssertWasCalled(x => x.DoFoo());
> >
> > > Hope this helps.
> > > Regards,
> > > David
> >
> > > On Wed, Aug 18, 2010 at 5:36 AM, samah <[email protected]>
> wrote:
> >
> > >> Hi all,
> >
> > >> How can we stub a method that returns nothing using Rhino Mocks?
> >
> > >> Thanks,
> >
> > >> -Samah
> >
> > >> --
> > >> You received this message because you are subscribed to the Google
> Groups
> > >> "Rhino.Mocks" group.
> > >> To post to this group, send email to [email protected].
> > >> To unsubscribe from this group, send email to
> > >> [email protected]<rhinomocks%[email protected]>
> <rhinomocks%2bunsubscr...@googlegrouÂps.com>
> > >> .
> > >> For more options, visit this group at
> > >>http://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 [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<rhinomocks%[email protected]>
> <rhinomocks%2bunsubscr...@googlegrouÂps.com>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/rhinomocks?hl=en.
> >
> > --
> > Tim Barcz
> > Microsoft C# MVP
> > Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://
> www.twitter.com/timbarcz- Hide quoted text -
> >
> > - Show quoted text -
>
> --
> You received this message because you are subscribed to the Google Groups
> "Rhino.Mocks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rhinomocks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rhinomocks?hl=en.
>
>
--
Tim Barcz
Microsoft C# MVP
Microsoft ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz
--
You received this message because you are subscribed to the Google Groups
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rhinomocks?hl=en.