Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock an update method returning a void with Moq

Tags:

c#

.net

mocking

moq

In my test, I defined as data a List<IUser> with some record in.

I'd like setup a moq the methode Update, this method receive the user id and the string to update.

Then I get the the IUser and update the property LastName

I tried this :

namespace Tests.UnitTests {     [TestClass]     public class UsersTest     {         public IUsers MockUsersRepo;         readonly Mock<IUsers> _mockUserRepo = new Mock<IUsers>();         private List<IUser> _users = new List<IUser>();          [TestInitialize()]         public void MyTestInitialize()         {             _users = new List<IUser>                 {                     new User { Id = 1, Firsname = "A", Lastname = "AA", IsValid = true },                     new User { Id = 1, Firsname = "B", Lastname = "BB", IsValid = true }                 };              Mock<IAction> mockUserRepository = new Mock<IAction>();             _mockUserRepo.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))                 .Returns(???);              MockUsersRepo = _mockUserRepo.Object;         }          [TestMethod]         public void Update()         {             //Use the mock here         }      } } 

But I get this error : cannot resolve Returns symbole

Do you have an id ?

class User : IUser {     public int Id { get; set; }     public string Firsname { get; set; }     public string Lastname { get; set; }     public bool IsValid { get; set; } }  interface IUser {     int Id { get; set; }     string Firsname { get; set; }     string Lastname { get; set; }     bool IsValid { get; set; } }  interface IAction {     List<IUser> GetList(bool isActive);     void Update(int id, string lastname) }  class Action : IAction {     public IUser GetById(int id)     {         //....     }     public void Update(int id, string lastname)     {         var userToUpdate = GetById(id);         userToUpdate.LastName = lastname;         //....     } } 
like image 323
Kris-I Avatar asked Apr 04 '13 08:04

Kris-I


People also ask

How do you mock method which returns void in C#?

Mockito provides following methods that can be used to mock void methods. doAnswer() : We can use this to perform some operations when a mocked object method is called that is returning void. doThrow() : We can use doThrow() when we want to stub a void method that throws exception.

What can be mocked with Moq?

Unit testing is a powerful way to ensure that your code works as intended. It's a great way to combat the common “works on my machine” problem. Using Moq, you can mock out dependencies and make sure that you are testing the code in isolation.

Can you mock a class with Moq?

You can use Moq to create mock objects that simulate or mimic a real object. Moq can be used to mock both classes and interfaces. However, there are a few limitations you should be aware of. The classes to be mocked can't be static or sealed, and the method being mocked should be marked as virtual.

How do you mock a method in C#?

Trying to mock a method that is called within another method. // code part public virtual bool hello(string name, int age) { string lastName = GetLastName(); } public virtual string GetLastName() { return "xxx"; } // unit test part Mock<Program> p = new Mock<Program>(); p. Setup(x => x. GetLastName()).


1 Answers

If you just want to verify this method is called, you should use Verifiable() method.

_mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))                    .Verifiable(); 

If you also want to do something with those parameters, use Callback() first.

_mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))                    .Callback((int id, string lastName) => {                        //do something                        }).Verifiable(); 

Update

Here's how you should mock it if you return a bool value as result.

_mockUserRepository.Setup(mr => mr.Update(It.IsAny<int>(), It.IsAny<string>()))                    .Returns(true); 
like image 134
Ufuk Hacıoğulları Avatar answered Sep 20 '22 23:09

Ufuk Hacıoğulları