Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Really basic moq example fails

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

like image 695
Morten Avatar asked Dec 07 '22 11:12

Morten


1 Answers

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).

like image 105
Sergey Berezovskiy Avatar answered Dec 10 '22 03:12

Sergey Berezovskiy