Last post Mar 15, 2019 05:06 AM by DA924
Mar 13, 2019 04:24 PM|itsmeabhilashgk|LINK
I am trying to write unit tests for my controller action. I am using NUnit 3 and Moq.
I have Db methods such as Add() and Remove() in the action.
I understand that I need to mock the Db and setup return values to the methods accordingly.
However when it comes to methods such as db.Add(), db.Remove() , db.SaveChanges(); I don't understand how to setup true and false return values for the same.
My action to be tested:
public List<string> Add([FromBody]model addModel)
List<string> errors = new List<string>();
model Model= new model()
propertyA = addModel.propertyA
catch (Exception e)
To write positive and negative tests for the above methods, I need to be able to set positive and negative return values for db.Add() and db.SaveChanges() methods.
I have tried the following:
public void Add_Succesfull_True()
Mock<DbContext> mockdb = new Mock<DbContext>();
var model= db.model.Select(x => x).FirstOrDefault();
model addModel = new model()
AuthController authController = new AuthController(userManager, configuration, mockdb.Object);
var apiEndPoint = authController.Add(addModel);
mockdb.Setup(x => x.Add(addModel)).Returns(??);
Through intellisense I can see that db.Add() expects a return type of
My understand of mocking the DbContext is as follows:
I believe I have mocked the above in a wrong way. Need direction thank you:)
Mar 13, 2019 05:59 PM|DA924|LINK
You could easily mock out data persistence if you were using the Repository or Data Access Object pattern, because the Interface can be mocked out and the persistence never actually done. IMO unit testing EF is a waste of time However, there is nothing wrong
in doing integration or functional testing.
The blow code is using the DAO pattern that can be mocked out, because DaoTask is using an Interface. Also, you'll notice there is not tyr/catch not even in the code of thte DAO, becuase of global exception handling.
public void CreateTask(DtoTask dto)
Mar 13, 2019 06:08 PM|itsmeabhilashgk|LINK
Mar 13, 2019 06:32 PM|DA924|LINK
Hi, I am not trying to test EF, I need to mock the return values of the DbSet methods such as Add(), so that I can test the flow of my function.
I cannot refactor now; given the current code; is there a possible way?
If you're not using EF, then what are you using? Regardless of how DBset is being used, I wouldn't waste time trying to UT DbSet, IMO.
Mar 13, 2019 06:34 PM|mgebhard|LINK
The concept is covered in the EF Core docs.
Mar 13, 2019 08:14 PM|bruce (sqlwork.com)|LINK
as you are not using the return value in your controller you can just use null:
mockdb.Setup(x => x.Add(addModel)).Returns((Microsoft.EntityFrameworkCore.ChangeTracking) null);
Mar 14, 2019 07:27 AM|itsmeabhilashgk|LINK
Hi I am using EF core and I am getting an error saying :
Microsoft.EntityFrameworkCore.ChangeTracking is a namespace used like a type
Is this because of proxy not being supported in EF Core?
Mar 15, 2019 05:06 AM|DA924|LINK
If you want to unit test the controller and use mock, then you will find a way to use the Repository or DAO patterns that are using an Interface and doing CRUD or use Model objects in the Models folder that are using an Interface that are doing CRUD so that
they can be mocked out.
You arrange, act and assert. You make the test fail, you make the test pass and then you refactor.