0

I am doing a unit test project to test a read method in a controller. I am trying to mock the Repository but I am getting a problem.

There is an App Repository which contains all the Repositories. and another Repository for users:

var repositoriesMock = new Mock<IAppRepositories>();
var userMock = new Mock<UserRepository> ();
repositoriesMock.SetupGet(x => x.Users).Returns(
                    partnerMock.Object
                );

userMock.SetupGet(x => x.GetUserDto(false, 1) ).Returns(
                    new List<GeschaeftspartnerDto> {user1, user2}.AsQueryable()
                );

in the controller:

public ActionResult Read([DataSourceRequest] DataSourceRequest request)
{
    var list = this.AppRepositories.Users.GetUserDto(false, 1).ToList();

}

but I am receiving an Error in uerMock.SetupGet

Expression is not a property access: x => x.GetUserDto(False, 1)

what is going on? why am I getting this error?

3
  • Show the definition for GetUserDto and its declaring type UserRepository. Does UserRepository have a backing interface? Commented Feb 28, 2017 at 12:56
  • I did not understand what you mean. Commented Feb 28, 2017 at 12:59
  • You have not provided enough details about the object in order to be able to help you properly. provide a minimal reproducible example that can be used to reproduce your problem and find a solution. Commented Feb 28, 2017 at 13:13

1 Answer 1

1

Try using Mock.Setup instead of Mock.SetupGet.

Sign up to request clarification or add additional context in comments.

3 Comments

now I am receiving a different error:Invalid setup on a non-virtual (overridable in VB) member: x => x.GetUserDto(False, 1)
You need to mark your UserRepository.GetUserDto as virtual or else mock an interface of the user repository instead of the concrete class.
how should I do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.