Indeed, the basic moq example does not work

I am trying to make Moq and I am stuck in a very simple example. I want to make fun of the very simple IInput interface:

namespace Example { public interface IInput { int SomeProperty { get; set; } } } 

This seems like a very simple task. However, I get a compilation error when I try to make fun of 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: β€œIt is not possible to convert from Moq.Mock<Example.IInput> to <Example.IInput> . I don’t see what I'm doing wrong. Please help me

+4
source share
2 answers

Use the Object property of the mock to retrieve an instance of the manufactured object.

  var x = new FirstClass(mock.Object); 

Within the framework of Moq, Mock not an example of what you are kidding (for example, in Rhino Mocks).

+12
source

Use the Object property in the Mock instance to get the actual mocked object.

 var x = new FirstClass(mock.Object); 
Class

Mock used to set up methods / checks. You have to use Object accessor due to C # compiler restriction. You can vote for what it raised on Microsoft Connect (see Note on QuickStart ).

+3
source

Source: https://habr.com/ru/post/1441488/


All Articles