I am trying out Moq, and I've gotten stuck in a very basic example. I want to mock a very simple interface IInput:
namespace Example
{
public interface IInput
{
int SomeProperty { get; set; }
}
}
This seems like a very easy job. However, I get a compilation error when I try to mock it in the following test code:
using Moq;
using NUnit.Framework;
namespace FirstEniro._Test
{
[TestFixture]
class TestFirstClass
{
[Test]
public void TestConstructionOk()
{
var mock = new Mock<IInput>();
mock.Setup(r => r.SomeProperty).Returns(3);
var x = new FirstClass(mock);
Assert.That(x, Is.EqualTo(3));
}
}
}
The compiler says "cannot convert from Moq.Mock<Example.IInput>
to <Example.IInput>
.
I can't see what I am doing wrong. Please help me
Use Object
property of mock to retrieve instance of mocked object.
var x = new FirstClass(mock.Object);
In Moq framework Mock
is not an instance of what you are mocking (like in Rhino Mocks).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With