SmallestWish
SmallestWish

Reputation: 115

Return is always null moq

I have invested so much time and have seen the different answers but dont know whats wrong, no matter what I do, I always get the Return value null.

I want the Return value to be an object (mock object or etc.), on which I could perform Assert operations. If I am on the wrong track of understanding the concept please tell me.

[Fact]
public void ProjectTest()
{
    var ProjectServiceMock = new Mock<IProjectService>();
    var ProjectMock = new Mock<IProject>();
    IProject project = ProjectMock.Object;
    ProjectServiceMock.Setup(x => x.CreateProject(new Path("C:"), "S1")).Returns(project);
    var addProjectViewModel = new AddProjectViewModel(ProjectServiceMock.Object);

    IProjectService obj = ProjectServiceMock.Object;
    var result = obj.CreateProject(new Path("C:"), "S1");
}

the result is always null.

Upvotes: 2

Views: 95

Answers (1)

Nkosi
Nkosi

Reputation: 246998

You are passing two separate instance between the setup and the invocation of the mock so it will return null by default.

You either use the same instance

[Fact]
public void ProjectTest() {
    var ProjectServiceMock = new Mock<IProjectService>();
    var ProjectMock = new Mock<IProject>();
    IProject project = ProjectMock.Object;
    var path = new Path("C:");
    ProjectServiceMock.Setup(x => x.CreateProject(path, "S1")).Returns(project);
    var addProjectViewModel = new AddProjectViewModel(ProjectServiceMock.Object);

    IProjectService obj = ProjectServiceMock.Object;
    var result = obj.CreateProject(path, "S1");
}

Or use a parameter matcher of it does not matter the actual argument instance

[Fact]
public void ProjectTest() {
    var ProjectServiceMock = new Mock<IProjectService>();
    var ProjectMock = new Mock<IProject>();
    IProject project = ProjectMock.Object;
    ProjectServiceMock.Setup(x => x.CreateProject(It.IsAny<Path>(), "S1")).Returns(project);
    var addProjectViewModel = new AddProjectViewModel(ProjectServiceMock.Object);

    IProjectService obj = ProjectServiceMock.Object;
    var result = obj.CreateProject(new Path("C:"), "S1");
}

That done, it is observed that are a basically trying to test the mocking framework. No actual code is being tested in the examples provided.

Try following the AAA approach with your test

[Fact]
public void ProjectTest() {
    //Arrange
    var ProjectServiceMock = new Mock<IProjectService>();
    var ProjectMock = new Mock<IProject>();
    IProject project = ProjectMock.Object;
    ProjectServiceMock
        .Setup(x => x.CreateProject(It.IsAny<Path>(), "S1"))
        .Returns(project);
    //System under test
    var addProjectViewModel = new AddProjectViewModel(ProjectServiceMock.Object);

    //Act
    addProjectViewModel.SomeMethodToTest();
    //...assumption is that `CreateProject(new Path("C:"), "S1")` will get called within
    //...the method under test

    //Assert
    //...now assert expected behavior        
}

Reference Moq Quickstart to get a better understanding of how to use that mocking framework

Upvotes: 1

Related Questions