Finished all controller unit tests

pull/1135/head
Steve Smith 2016-03-13 14:35:38 -04:00
parent 82fe3ad532
commit 29130330da
5 changed files with 145 additions and 1 deletions

View File

@ -26,7 +26,13 @@ namespace TestingControllersSample.Api
{
return HttpNotFound(sessionId);
}
return new ObjectResult(session.Ideas.Select(i => new { id = i.Id, name=i.Name, description = i.Description, dateCreated = i.DateCreated}));
return new ObjectResult(session.Ideas.Select(i => new
{
id = i.Id,
name =i.Name,
description = i.Description,
dateCreated = i.DateCreated
}));
}
public class NewIdeaModel

View File

@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("TestingControllerSample.Tests")]

View File

@ -14,6 +14,9 @@
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<ProduceOutputsOnBuild>True</ProduceOutputsOnBuild>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc;
using Moq;
using TestingControllersSample.Api;
using TestingControllersSample.Core.Interfaces;
using TestingControllersSample.Core.Model;
using Xunit;
namespace TestingControllerSample.Tests.UnitTests
{
public class ApiIdeasControllerCreate
{
[Fact]
public void ReturnsBadRequestGivenInvalidModel()
{
var mockRepo = new Mock<IBrainStormSessionRepository>();
var controller = new IdeasController(mockRepo.Object);
controller.ModelState.AddModelError("error","some error");
var result = Assert.IsType<BadRequestObjectResult>(controller.Create(null));
}
[Fact]
public void ReturnsHttpNotFoundForInvalidSession()
{
var mockRepo = new Mock<IBrainStormSessionRepository>();
int testSessionId = 123;
mockRepo.Setup(r => r.GetById(testSessionId)).Returns((BrainStormSession)null);
var controller = new IdeasController(mockRepo.Object);
var result = Assert.IsType<HttpNotFoundObjectResult>(controller.Create(new IdeasController.NewIdeaModel()));
}
[Fact]
public void ReturnsNewlyCreatedIdeaForSession()
{
var mockRepo = new Mock<IBrainStormSessionRepository>();
int testSessionId = 123;
string testName = "test name";
string testDescription = "test description";
var testSession = GetTestSession();
mockRepo.Setup(r => r.GetById(testSessionId)).Returns(testSession);
var controller = new IdeasController(mockRepo.Object);
var newIdea = new IdeasController.NewIdeaModel()
{
Description = testDescription,
Name = testName,
SessionId = testSessionId
};
mockRepo.Setup(r => r.Update(testSession)).Verifiable();
var result = Assert.IsType<ObjectResult>(controller.Create(newIdea));
var returnSession = result.Value as BrainStormSession;
mockRepo.Verify();
Assert.Equal(2, returnSession.Ideas.Count());
Assert.Equal(testName, returnSession.Ideas.LastOrDefault().Name);
Assert.Equal(testDescription, returnSession.Ideas.LastOrDefault().Description);
}
private BrainStormSession GetTestSession()
{
var session = new BrainStormSession()
{
DateCreated = new DateTime(2016, 7, 2),
Id = 1,
Name = "Test One"
};
var idea = new Idea() {Name = "One"};
session.AddIdea(idea);
return session;
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc;
using Moq;
using TestingControllersSample.Api;
using TestingControllersSample.Core.Interfaces;
using TestingControllersSample.Core.Model;
using Xunit;
namespace TestingControllerSample.Tests.UnitTests
{
public class ApiIdeasControllerForSession
{
[Fact]
public void ReturnsHttpNotFoundForInvalidSession()
{
var mockRepo = new Mock<IBrainStormSessionRepository>();
int testSessionId = 123;
mockRepo.Setup(r => r.GetById(testSessionId)).Returns((BrainStormSession)null);
var controller = new IdeasController(mockRepo.Object);
var result = Assert.IsType<HttpNotFoundObjectResult>(controller.ForSession(testSessionId));
}
[Fact]
public void ReturnsIdeasForSession()
{
var mockRepo = new Mock<IBrainStormSessionRepository>();
int testSessionId = 123;
mockRepo.Setup(r => r.GetById(testSessionId)).Returns(GetTestSession());
var controller = new IdeasController(mockRepo.Object);
var result = Assert.IsType<ObjectResult>(controller.ForSession(testSessionId)).Value as IEnumerable<dynamic>;
dynamic idea = result.FirstOrDefault();
// this requires InternalsVisibleTo on the SUT project
Assert.Equal("One", idea.name);
}
private BrainStormSession GetTestSession()
{
var session = new BrainStormSession()
{
DateCreated = new DateTime(2016, 7, 2),
Id = 1,
Name = "Test One"
};
var idea = new Idea() {Name = "One"};
session.AddIdea(idea);
return session;
}
}
}