I am trying to mock a method that updates an entry into the database using EF.
class MyController
{
private readonly IEmployeeRepository;
private readonly IProjectRepository;
MyController(IEmployeeRepository employeeRepository,IProjectRepository projectRepository)
{
_employeeRepository=employeeRepository;
_projectRepository=projectRepository;
}
public async Task<IHttpActionResult> ExtendEmployeeContract(int employeeId, int projectId)
{
var projectModel=_projectRepository.GetProjectDetails(projectId);
int result=-1;
if(projectMode !=null)
{
bool isprojectactive=projectMode.IsActive;
// Some made-up function that is setting the IsContractActive value to true/false
result=_employeeRepository.ExtendContract
(new EmployeeModel(){Id = employeeId,IsContractActive = isprojectactive};);
}
return Ok(new ResponseModel(){Data=result});
}
}
[TestClass]
class MyControllerTestClass
{
private Mock<IEmployeeRepository> _mockedEmployeeRepository;
private Mock<IProjectRepository> _mockedProjectRepository;
ProjectModel _projectModel;
EmployeeModel _empModel;
[TestInitialize]
public void TestInitialize()
{
_mockedEmployeeRepository = new Mock<IEmployeeRepository>();
_mockedProjectRepository = new Mock<IProjectRepository>();
_projectModel = new ProjectModel()
{
Id = 5678,
Name = "testProject",
IsActive=true;
};
_empModel = new EmployeeModel()
{
Id = 123,
IsContractActive = false
};
}
[TestMethod]
public async Task ExtendEmployeeContract_ShouldUpdateAndExtend_EmployeeContract()
{
// Arrange
_mockedProjectRepository.Setup(x=>x.GetProjectDetails(5678)).Returns(_projectModel);
_mockedEmployeeRepository.Setup(x=>x.ExtendContract(_empModel)).Returns(1);
// Act
var mycontroller=new MyController(_mockedEmployeeRepository.object,_mockedProjectRepository.object);
var actionResult = await mycontroller.ExtendEmployeeContract(123,5678) as OkNegotiatedContentResult<ResponseModel>;
// Assert
Assert.AreEqual(1,Convert.ToInt(actionResult.Content.Data.ToString());
}
}
I am trying to mock this controller in my test class . I can successfully mock GetProjectDetails method but I cant mock ExtendContract. I debugged the ExtendEmployeeContract method and found that _employeeRepository.ExtendContract always returns 0
Your fake setup is wrong. Even though you are passing your EmployeeModel
_mockedEmployeeRepository.Setup(x=>x.ExtendContract(_empModel)).Returns(1);
In your controller code you are creating new Employee model, which is different from what you pass in test
result=_employeeRepository.ExtendContract
(new EmployeeModel(){Id = employeeId,IsContractActive = isprojectactive};);
Your fake setup should look like:
_mockedEmployeeRepository.Setup(x=>x.ExtendContract(It.IsAny<EmployeeModel>())).Returns(1);
Related
I am having the a typical CRUD operation interface (repository) and i was wondering how would someone test it using MOQ.
Model
public class Model
{
public int Id{get;set;}
}
Interface
public interface ICrud
{
Task<IEnumerable<Model>> GetAllAsync();
Task AddAsync(Model model);
}
Service
public class Service
{
public ICrud operations;
Service(ICrud crud){ this.operations=crud;}
public Task<IEnumerable<Model>> GetAllAsync()=>this.operations.GetAllAsync();
public Task AddAsync(Model model)=> this.operations.AddAsync(model);
}
Unit Test
public class Test
{
public IEnumerable Seed(){
yield return new Model {id=3};
yield return new Model {id =4};
}
[Testcase(3)]
public async Task CanAdd(int id)
{
var mock=new Mock<ICrud>();
var newModel=new Model{ Id=id};
mock.Setup(x=>x.GetAsync()).ReturnsAsync(Seed);
mock.Setup(x=>x.AddAsync(newModel));
//how can i test adding the new model
var service=new Service(mock.Object);
var initialList=await service.GetAllAsync();
//adding
await service.AddAsync(newModel);
var finalList=await service.GetAllAsync();
}
}
My question is , how can i test the following scenario:
-i check the initial collection
-i call `AddAsync`
-i check to see that the new collection contains the added element.
How can this be achieved with Moq in a unit test?
Or you can do it without mocking frameworks.
public class InMemoryCrud : ICrud
{
public List<Model> Models { get; set; } = new List<Model>();
public Task<IEnumerable<Model>> GetAllAsync() => return Task.FromResult(Models);
public Task AddAsync(Model model)
{
Models.Add(model);
return Task.CompletedTask;
}
}
public async Task Add_Model()
{
var fakeCrud = new InMemoryCrud();
var service = new Service(fakeCrud);
var newModel = new Model { Id = 3 };
await service.AddAsync(newModel);
var actualModels = await fakeCrud.GetAllAsync();
var expected = new[]
{
new Model { Id = 3 }
}
actualModels.Should().BeEquivalentTo(expected); // Pass
}
With InMemoryCrud implementation you can test that correct values ahs been "saved" via crud operations.
With mocking frameworks you will test that correct methods has been called. For example if in Service class I change some properties of the given instance of the Model - tests still pass, but wrong data will be saved to the database in real application.
In this scenario, pass case is that the subject service under test correctly invokes the dependency operation with the given model.
The test should thus reflect that when being exercised.
Using MOQ that would look like
public async Task Service_Should_AddAsync() {
//Arrange
int id = 1;
var mock = new Mock<ICrud>();
var newModel = new Model { Id = id };
mock.Setup(x => x.AddAsync(It.IsAny<Model>())).Returns(Task.CompletedTask);
var service = new Service(mock.Object);
//Act
await service.AddAsync(newModel);
//Assert
//verify that the mock was invoked with the given model.
mock.Verify(x => x.AddAsync(newModel));
}
I want to test GetMoviesAsync of my Controller. I don't know where I am doing wrong in my Moq setup. I am getting 0 item from GetMoviesAsync.
What am I doing wrong?
// Api-Controller:
public interface ICommand
{
Task<IEnumerable<Movie>> GetMoviesAsync();
}
public class SampleController : ControllerBase
{
private readonly ICommand movieCommand;
public SampleController(ICommand command)
{
movieCommand = command;
}
[HttpGet]
public async Task<IActionResult> GetMoviesAsync()
{
var movies = await movieCommand.GetMoviesAsync();
return Ok(movies);
}
}
// Unit-Test:
public class SampleControllerTest
{
private IEnumerable<Movie> MovieList()
{
IList<Movie> movies = new List<Movie>()
{
new Movie()
{
ID =1,
Title = "Test",
ReleaseDate = DateTime.Now,
RunningTimeInMinutes = 100
}
};
return movies;
}
private SampleController GetSampleController()
{
var command = new Mock<ICommand>();
return new SampleController(command.Object);
}
[Fact]
public async Task GetMovies_Test()
{
// Arrange
var controller = GetSampleController();
var commadMock = new Mock<ICommand>();
// How to setup moq here?
commadMock.Setup(s => s.GetMoviesAsync()).Returns(Task.FromResult<IEnumerable<Movie>>(MovieList())).Verifiable();
// Act
var response = await controller.GetMoviesAsync() as OkObjectResult;
// Problem is here,
var li=response.Value as IEnumerable<Movie>;
}
}
What am I doing wrong?
Two completely different mocks are being used.
One is used to create the controller
private SampleController GetSampleController()
{
var command = new Mock<ICommand>();
return new SampleController(command.Object);
}
and another is being created and setup in the test.
var controller = GetSampleController();
var commadMock = new Mock<ICommand>();
// How to setup moq here?
commadMock.Setup(s => s.GetMoviesAsync()).Returns(Task.FromResult<IEnumerable<Movie>>(MovieList())).Verifiable();
To solve this, use the same mock to get the desired behavior
[Fact]
public async Task GetMovies_Test() {
// Arrange
var commadMock = new Mock<ICommand>();
var controller = new SampleController(commadMock.Object); //<---
commadMock
.Setup(_ => _.GetMoviesAsync())
.ReturnsAsync(MovieList())
.Verifiable();
// Act
var response = await controller.GetMoviesAsync() as OkObjectResult;
//Assert
var list = response.Value as IEnumerable<Movie>;
//...
}
Note the use of ReturnsAsync to setup the returned Task
It seems that you are not using the correct mock on the Controller. The one that you are using does not have any setup on top of the method GetMoviesAsync
For me helped almost the solution offered by Nkosi but with little difference
[Fact]
public async Task GetMovies_Test() {
// Arrange
var commadMock = new Mock<ICommand>();
var controller = new SampleController(commadMock.Object); //<---
commadMock
.Setup(_ => _.GetMoviesAsync())
.ReturnsAsync(MovieList());
// Act
var response = await controller.GetMoviesAsync();
//Assert
var returnValue = Assert.IsType<ViewResult>(response);
var model = returnValue.Model as IEnumerable<Movie>;
//...
}
I'm a sitecore developer and I want to create a sample sitecore helix unit testing project for testing out our "HomeBottomContentController" controller:
public class HomeBottomContentController : GlassController
{
private readonly ISitecoreContext _iSitecoreContext;
public HomeBottomContentController(ISitecoreContext iSitecoreContext)
{
_iSitecoreContext = iSitecoreContext;
}
public override ActionResult Index()
{
var model = _iSitecoreContext.GetCurrentItem<Home_Control>();
return View("~/Views/HomeBottomContent/HomeBottomContent.cshtml", model);
}
}
I have created a WTW.Feature.HomeBottomContent.Tests project, for the purpose of testing this entire component using helix unit testing. In it I have a UnitTest1.cs file with following:
namespace WTW.Feature.HomeBottomContent.Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void Test_ISitecoreContextInsertion()
{
var iSitecoreContext = Mock.Of<Glass.Mapper.Sc.ISitecoreContext>();
HomeBottomContentController controllerUnderTest = new HomeBottomContentController(iSitecoreContext);
var result = controllerUnderTest.Index() as ViewResult;
Assert.IsNotNull(result);
}
}
}
This test does pass, meaning "result" is NOT null; however, the problem is when I step into the Index() code, I see that the "model" variable is NULL when we do
var model = _iSitecoreContext.GetCurrentItem<Home_Control>();
My question is, how exactly do I change this code to make sure that the "model" in that line does not become null? How do I "mock" an item in unit test code for the _iSitecoreContext so that it has a "Home_Control" template with legit values for its fields? Would that even be the right approach? Most online sources I've found do not have a similar scenario, I'm looking for the shortest code possible.
Another question I had is, how can I test the below Index() method in my [TestMethod], given that the SitecoreContext is declared inside the Index() method, rather than received in the HomeBottomContentController constructor like above? Is there a way to do that from the [TestMethod], or we have to send in the SitecoreContext into the HomeBottomContentController constructor or into the Index() method as a parameter?
public override ActionResult Index()
{
var context = new SitecoreContext();
var model = context.GetCurrentItem<Home_Control>();
return View("~/Views/HomeBottomContent/HomeBottomContent.cshtml", model);
}
In that case you would need to mock the desired behavior on the mocked dependency
[TestClass]
public class UnitTest1 {
[TestMethod]
public void Test_ISitecoreContextInsertion() {
//Arrange
var model = new Home_Control() {
//...populate as needed
}
var iSitecoreContext = new Mock<Glass.Mapper.Sc.ISitecoreContext>();
//Setup the method to return a model when called.
iSitecoreContext.Setup(_ => _.GetCurrentItem<Home_Control>()).Returns(model);
var controllerUnderTest = new HomeBottomContentController(iSitecoreContext.Object);
//Act
var result = controllerUnderTest.Index() as ViewResult;
//Assert
Assert.IsNotNull(result);
Assert.IsNotNull(result.Model);
//...other assertions.
}
}
UPDATE
Creating the context within the action tightly couples it to the context, making it almost impossible to mock. That is the reason explicit dependencies are injected
You can do something like that:
public class HomeBottomContentController : GlassController
{
private readonly ISitecoreContext _iSitecoreContext;
public HomeBottomContentController(ISitecoreContext iSitecoreContext)
{
_iSitecoreContext = iSitecoreContext;
}
public override ActionResult Index()
{
var model = this.GetCurrentItem();
return View("~/Views/HomeBottomContent/HomeBottomContent.cshtml", model);
}
protected virtual Home_Control GetCurrentItem()
{
return _iSitecoreContext.GetCurrentItem<Home_Control>();
}
}
namespace WTW.Feature.HomeBottomContent.Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void Test_ISitecoreContextInsertion()
{
var iSitecoreContext = Mock.Of<Glass.Mapper.Sc.ISitecoreContext>();
var controllerUnderTest = new FakeHomeBottomContentController(iSitecoreContext);
var result = controllerUnderTest.Index() as ViewResult;
Assert.IsNotNull(result);
}
}
public class FakeHomeBottomContentController : HomeBottomContentController
{
public FakeHomeBottomContentController(ISitecoreContext iSitecoreContext) : base(iSitecoreContext)
{
}
protected override Home_Control GetCurrentItem()
{
// return instance of Home_Control type
// e.g.
return new Home_Control();
}
}
}
I am trying to test a controller action that accepts a view model and creates a new entry. Here is the controller action:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ContactViewModel viewModel)
{
if (!ModelState.IsValid)
return View("Create", viewModel);
_unitOfWork.Contacts.Add(Mapper.Map(viewModel, new Contact()));
_unitOfWork.Complete();
return RedirectToAction("Index");
}
And the unit test:
[TestClass]
public class ContactControllerTests
{
private ContactsController _controller;
private Mock<IContactRepository> _mockRepository;
private string _userId;
private Mock<IUnitOfWork> _mockUoW;
[ClassInitialize]
public static void Init(TestContext context)
{
Mapper.Initialize(c => c.AddProfile<MappingProfile>());
}
[TestInitialize]
public void TestInitialize()
{
_userId = "1";
_mockRepository = new Mock<IContactRepository>();
_mockUoW = new Mock<IUnitOfWork>();
_mockUoW.SetupGet(u => u.Contacts).Returns(_mockRepository.Object);
_controller = new ContactsController(_mockUoW.Object);
_controller.MockCurrentUser(_userId, "user#domain.com");
}
[TestMethod]
public void CreatePost_ValidValuesSubmitted_ShouldCallComplete()
{
var viewModel = new ContactViewModel()
{
FirstName = "a",
LastName = "b"
};
_controller.Create(viewModel);
_mockRepository.Object.GetContacts(_userId).Should().HaveCount(1);
}
}
The unit test always returns the count 0 while I expect it to be 1. I am pretty new to TDD and I implemented unit of work and repository pattern as shown in Mosh Hamedani's course at:
https://app.pluralsight.com/library/courses/full-stack-dot-net-developer-architecture-testing/table-of-contents
You have not mocked any behavior for the repository in the above example.
Based on provided example, let's assume a simple interface like this.
public interface IContactRepository {
void Add(Contact contact);
IEnumerable<Contact> GetContacts(string _userId);
}
You need to have some form of storage for your data.
[TestInitialize]
public void TestInitialize() {
_userId = "1";
var data = new List<Contact>();//To store test data.
//Configure repository
_mockRepository = new Mock<IContactRepository>();
_mockRepository.Setup(m => m.Add(It.IsAny<Contact>())).Callback<Contact>(data.Add);
_mockRepository.Setup(m => m.GetContacts(_userId)).Returns(data);
//Configure UoW
_mockUoW = new Mock<IUnitOfWork>();
_mockUoW.SetupGet(u => u.Contacts).Returns(_mockRepository.Object);
_controller = new ContactsController(_mockUoW.Object);
_controller.MockCurrentUser(_userId, "user#domain.com");
}
or forego the mock and create a fake.
public class FakeContactRepository : IContactRepository {
private ICollection<Contact> data;
public FakeContactRepository(ICollection<Contact> data) {
this.data = data;
}
public void Add(Contact contact) {
data.Add(contact);
}
public IEnumerable<Contact> GetContacts(string _userId) {
return data;
}
}
and set it up for the test.
[TestInitialize]
public void TestInitialize() {
_userId = "1";
var data = new List<Contact>();//To store test data.
//Configure repository
var fakeRepository = new FakeContactRepository(data);
//Configure UoW
_mockUoW = new Mock<IUnitOfWork>();
_mockUoW.SetupGet(u => u.Contacts).Returns(fakeRepository );
_controller = new ContactsController(_mockUoW.Object);
_controller.MockCurrentUser(_userId, "user#domain.com");
}
There is bound to be something obvious I'm overlooking - there usually is when I have problems like this.
I have a controller that simply returns a news article based on a supplied ID:
[HandleError]
public class HomeController : Controller
{
private readonly IArticleRepository articleRepository;
public HomeController(IArticleRepository Repository)
{
articleRepository = Repository;
}
public ActionResult Index()
{
return View("Index");
}
// Here's the bit we're interested in
public ActionResult Article(int id)
{
var article = articleRepository.GetById(id);
return View("Article", article);
}
}
I'm mocking this using Moq like so:
[TestFixture]
public class HomeControllerTests
{
HomeController controller;
int articleId;
Article model;
[TestFixtureSetUp]
public void SetupMethods()
{
Mock<IArticleRepository> repositoryMock = new Mock<IArticleRepository>();
repositoryMock.Setup(x => x.GetById(articleId)).Returns(GetSampleArticle());
controller = new HomeController(repositoryMock.Object);
}
[Test]
public void Article_Action_Returns_Requested_Article()
{
// Arrange
model = new Article();
articleId = 1;
// Act
ActionResult result = controller.Article(articleId);
// Assert
var viewResult = ((ViewResult)result);
var returnedModel = viewResult.Model;
Assert.IsInstanceOf<Article>(viewResult.Model);
//Assert.AreEqual(articleId, returnedModel.ID);
}
}
The "GetSampleArticle" method in question above just looks like this:
private Article GetSampleArticle()
{
Article article = new Article()
{
Archived = false,
Body = "<p>This is a dummy sample article for use in our mocks.</p>",
EndDate = DateTime.Today.AddDays(30),
ID = 1,
Priority = 3,
StartDate = DateTime.Today,
Title = "Sample Article"
};
return article;
}
However I'm still getting a null type for the model. So what have I forgotten?
News.Tests.Controllers.HomeControllerTests.Article_Action_Returns_Requested_Article:
Expected: instance of <News.Data.Article>
But was: null
Another idea is to use the It.IsAny() method to avoid having to look for a hardcoded value at all:
repositoryMock.Setup(x => x.GetById(It.IsAny<int>())).Returns(GetSampleArticle());
Which is safe to do since you're not concerned with the actual value of the articleId so much as the mechanics of its retrieval.
In your SetupMethods, articleId is 0.
In your test, you set it to 1, so your .Setup is never being called. I would move your Setup into your test.
[Test]
public void Article_Action_Returns_Requested_Article()
{
// Arrange
model = new Article();
articleId = 1;
Mock<IArticleRepository> repositoryMock = new Mock<IArticleRepository>();
repositoryMock.Setup(x => x.GetById(articleId)).Returns(GetSampleArticle());
controller = new HomeController(repositoryMock.Object);
// Act
ActionResult result = controller.Article(articleId);
// Assert
var viewResult = ((ViewResult)result);
var returnedModel = viewResult.Model;
Assert.IsInstanceOf<Article>(viewResult.Model);
//Assert.AreEqual(articleId, returnedModel.ID);
}