Unit test using NUnit

May 16, 2012 at 4:40 PM

Hi, I'm writing the TDD to my app base on this project. I've got few problems testing a Create controller. I mocked up the repository, the ICommandBus and I pass these to the controller. When I execute the Create action method, this row always return false so the test fail: 

var result = _commandBus.Submit(command);

This is the test method:

 

[Test]
        public void Create()
        {

            const string expectedRouteName = "Index";
            
            // Mock the arguments
            var mockRepository = Substitute.For<ICompanyRepository>();
            var mockProcessor = Substitute.For<ICommandBus>();

            // Arrange
            var controller = new CompanyController(mockProcessor, mockRepository);

            // Act
            var company = new Company
            {
                Code = "7654321",
                CompanyId = 1,
                Name = "xxxxxx"
            };
            
            var result = controller.Create(company) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result, "Should return a ViewResult");
            Assert.AreEqual(expectedRouteName, result.RouteValues["action"], 
                "View name should have been '{0}'", expectedRouteName);

        }

 

And this is the controller action method:

[HttpPost]
        public  ActionResult Create(Company company)
        {
            
            // try to save the record
            if (ModelState.IsValid)
            {

                // create the command
                var command = new CreateOrUpdateCompanyCommand
                {
                    CompanyId = company.CompanyId,
                    Code = company.Code,
                    Name = company.Name
                };

                // check for errors
                IEnumerable<ValidationResult> errors = _commandBus.Validate(command);

                ModelState.AddModelErrors(errors);

                if (ModelState.IsValid)
                {
                    var result = _commandBus.Submit(command);
                    if (result.Success) return RedirectToAction("Index");
                }

            }

            // if fail
            return View("Create", company);

        }

Has anyone have an advise to give me? Thanks.

May 17, 2012 at 8:13 AM

I've got the answer. Specify that I'm using NSubstitute (http://nsubstitute.github.com) as mock framework. The previous test should be like this:

[Test]
        public void Create()
        {

            const string expectedRouteName = "Index";
            
            // Mock the arguments
            var mockRepository = Substitute.For<ICompanyRepository>();
            var mockProcessor = Substitute.For<ICommandBus>();

            // Fake the result
            var resultCommand = new CommandResult(true);
            mockProcessor
                .Submit(Arg.Any<CreateOrUpdateCompanyCommand>())
                .Returns(resultCommand);

            // Arrange
            var controller = new CompanyController(mockProcessor, mockRepository);

            // Act
            var company = new Company
            {
                Code = "XXXXXXX",
                CompanyId = 1,
                Name = "My Company"
            };
            
            var result = controller.Create(company) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result, "Should return a ViewResult");
            Assert.AreEqual(expectedRouteName, result.RouteValues["action"], 
                "View name should have been '{0}'", expectedRouteName);

        }

 

This works fine. It's necessary to set to the processor that any command passed through, It must result what you want ( var resultCommand = new CommandResult(true); ).

Hope this can help anyone is stumble on tests of this project.

.