Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to assign values to properties in moq?
    text
    copied!<p>I have a class with a method that returns an object of type <code>User</code></p> <pre><code>public class CustomMembershipProvider : MembershipProvider { public virtual User GetUser(string username, string password, string email, bool isApproved) { return new User() { Name = username ,Password = EncodePassword(password) ,Email = email ,Status = (isApproved ? UsuarioStatusEnum.Ativo : UsuarioStatusEnum.ConfirmacaoPendente) // ... }; } // .. } </code></pre> <p><code>User</code> is a domain object. Note the <code>Id</code> property with <strong>setter as protected</strong>:</p> <pre><code>public class User : IAuditable, IUser { public virtual int Id { get; protected set; } public virtual string Name { get; set; } public virtual string Email { get; set; } public virtual UsuarioStatusEnum Status { get; set; } public virtual string Password { get; set; } } </code></pre> <p><em>Id is protected because it is generated by the database.</em></p> <h2>Test project</h2> <p>In my Test project I have a Fake repository with a method <code>Store</code> to save/update the object:</p> <pre><code>public void Store(T obj) { if (obj.Id &gt; 0) _context[obj.Id] = obj; else { var generateId = _context.Values.Any() ? _context.Values.Max(p =&gt; p.Id) + 1 : 1; var stubUser = Mock.Get&lt;T&gt;(obj); // In test, will always mock stubUser.Setup(s =&gt; s.Id).Returns(generateId); _context.Add(generateId, stubUser.Object); } } </code></pre> <p>In <code>CustomMembershipProvider</code> I have <code>public override MembershipUser CreateUser</code> method that calls the <code>GetUser</code> to create a <code>User</code>.<br> This way, all I have to do is mock the <code>GetUser</code> method so that the repository can generate the <code>Id</code></p> <pre><code>var membershipMoq = new Mock&lt;CustomMembershipProvider&gt;(); membershipMoq.CallBase = true; membershipMoq .Setup(p =&gt; p.GetUser(It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;(), It.IsAny&lt;bool&gt;())) .Returns&lt;string, string, string, bool&gt;( (username, password, email, isAproved) =&gt; { var moqUser = new Mock&lt;User&gt;(); moqUser.Object.Name = username; moqUser.Object.Password = password; moqUser.Object.Email = email; moqUser.Object.Status = (isAproved ? UsuarioStatusEnum.Ativo : UsuarioStatusEnum.ConfirmacaoPendente); return moqUser.Object; }); _membershipProvider = membershipMoq.Object; </code></pre> <h2>Problem</h2> <p>In theory everything is correct. When <code>CreateUser</code> call 'GetUser' to create a user, the user will return Mock filled;</p> <pre><code>[TestMethod] public void CreateUser_deve_criar_usuario_no_repositorio() { // Act MembershipCreateStatus status; var usr = _membershipProvider.CreateUser( _fixture.Create&lt;string&gt;(), _fixture.Create&lt;string&gt;(), _fixture.Create&lt;string&gt;(), null, null, true, null, out status); // usr should have name, email password filled. But not! // Assert status.Should().Be(MembershipCreateStatus.Success); } </code></pre> <p>The problem is that Email, Name, Password are empty (with default values)!</p> <p><img src="https://i.stack.imgur.com/WTpee.jpg" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload