Recursive Mocks

33 views
Skip to first unread message

Richard Banks

unread,
Jul 5, 2010, 6:34:19 AM7/5/10
to nsubs...@googlegroups.com
In Rhino I can do this:

		[Fact]
public void Rhino_RecursiveMocks()
{
var monkey = MockRepository.GenerateMock<IMonkey>();
monkey.Expect(m => m.Name).PropertyBehavior();
monkey.Name = "Spike";
monkey.Expect(m => m.Keeper().AssignedMonkey).Return(monkey);
Assert.NotNull(monkey.Keeper());
Assert.Equal("Spike", monkey.Keeper().AssignedMonkey.Name);
}

But the following in NSub doesn't work

		[Fact]
public void NSubstitute_RecursiveMocks()
{
var monkey = Substitute.For<IMonkey>();
monkey.Keeper().AssignedMonkey.Returns(monkey);
monkey.Name = "Spike";
Assert.NotNull(monkey.Keeper());
Assert.Equal("Spike",monkey.Keeper().AssignedMonkey.Name);
}
Relevant code for mocks is:
	public interface IMonkey
{
string Name { getset; }
IZooKeeper Keeper();
}
	public interface IZooKeeper
{
IMonkey AssignedMonkey { getset; }
}


--
- Richard Banks (@rbanks54)
blog: http://www.richard-banks.org
podcast: http://www.talkingshopdownunder.com

David Tchepak

unread,
Jul 5, 2010, 6:57:55 AM7/5/10
to nsubs...@googlegroups.com
Hi Richard,

Recursive mocks aren't implemented yet, but are planned for the 1.0 release.
Another 1.0 feature currently missing is ordered mocks.

Regards,
David

--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To post to this group, send email to nsubs...@googlegroups.com.
To unsubscribe from this group, send email to nsubstitute...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nsubstitute?hl=en.

Anthony Egerton

unread,
Jul 5, 2010, 7:04:39 AM7/5/10
to nsubs...@googlegroups.com
I would've said a future feature, rather than commiting myself to a specific release.
That way we could come up with new versions with extra features. ;)

Richard Banks

unread,
Jul 5, 2010, 7:19:08 AM7/5/10
to nsubs...@googlegroups.com
It's on the internet now, and the internet never forgets!! :-)

I'm sure we won't hold you to it.

David Tchepak

unread,
Jul 5, 2010, 7:49:19 AM7/5/10
to nsubs...@googlegroups.com
Cue a Google-style perpetual beta! :P

This is what I get for not letting the product owner do the talking. I'm so fired. ;)

David Tchepak

unread,
Jul 17, 2010, 10:15:58 AM7/17/10
to nsubs...@googlegroups.com
Hi Richard,

I've just checked in a first attempt at recursive mocking for members that return interfaces.

Some examples here: http://bit.ly/bVddbO

It would be awesome if you could do a build from the repo, try it out on your example and see if it works the way you want it to. (Otherwise I'll give it a go once you post the code from your DDDSydney session.)

Cheers,
David


On Mon, Jul 5, 2010 at 8:34 PM, Richard Banks <rban...@gmail.com> wrote:

--

Richard Banks

unread,
Jul 18, 2010, 11:46:26 PM7/18/10
to nsubs...@googlegroups.com
Will do.

Richard Banks

unread,
Jul 19, 2010, 12:00:35 AM7/19/10
to nsubs...@googlegroups.com
And done.

And working (using that code I showed demoed Saturday)

Nice stuff :-)
Reply all
Reply to author
Forward
0 new messages