Reputation: 21615
I'm having problems mocking an set of interfaces:
interface IFoo
{
object Blah { get; }
}
interface IBar : IFoo
{
new string Blah { get; }
}
I tried mocking as:
var mock = new Mock<IFoo>();
mock.SetupGet(m => m.Blah).Returns("Blah");
This works now:
Assert.That(mock.Object.Blah, Is.EqualTo("Blah"));
The problem is that when I add the following, to also have a value for the IBar
interface, the value of IFoo.Blah
is null.
var bar = mock.As<IBar>();
bar.SetupGet(m => m.Blah).Returns("Blah");
This works now:
Assert.That(((IBar)mock.Object).Blah, Is.EqualTo("Blah"));
But the old one fails:
Assert.That(mock.Object.Blah, Is.EqualTo("Blah"));
// mock.Object.Blah is null now
Is there a way to get both to work?
Upvotes: 2
Views: 191
Reputation: 1389
With MOQ (v4.0.10827) and .Net 4, this code is working:
var mock = new Mock<IFoo>();
var bar = mock.As<IBar>();
mock.SetupGet(m => m.Blah).Returns("Blah");
Assert.That(mock.Object.Blah, Is.EqualTo("Blah"));
bar.SetupGet(m => m.Blah).Returns("BlahBlah");
Assert.That(((IBar)mock.Object).Blah, Is.EqualTo("BlahBlah"));
Assert.That(mock.Object.Blah, Is.EqualTo("Blah"));
You must initialize your interface before setting up your property.
Upvotes: 1
Reputation: 1799
I think you need to use "override" instead of "new". When you use "new", you're actually making a second function. Here's the doc: http://msdn.microsoft.com/en-us/library/ms173153.aspx
Upvotes: 0