How to assign values to properties in moq?

后端 未结 2 752
我在风中等你
我在风中等你 2020-12-04 21:18

I have a class with a method that returns an object of type User

public class CustomMembershipProvider : MembershipProvider
{
    public virtual         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 22:17

    The way you prepare the mocked user is the problem.

    moqUser.Object.Name = username;
    

    will not set the name, unless you have setup the mock properly. Try this before assigning values to properties:

    moqUser.SetupAllProperties();
    

    This method will prepare all properties on the mock to be able to record the assigned value, and replay it later (i.e. to act as real property).

    You can also use SetupProperty() method to set up individual properties to be able to record the passed in value.

    Another approach is:

    var mockUser = Mock.Of( m =>
        m.Name == "whatever" &&
        m.Email == "someone@example.com"); 
    
    return mockUser;
    

提交回复
热议问题