Created
August 8, 2016 19:09
-
-
Save dsolovay/38188bbf6155c2910edcf44f24281945 to your computer and use it in GitHub Desktop.
Demo of adding functionality to MVC controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using FluentAssertions; | |
using MvcSample.Controllers; | |
using NSubstitute; | |
using Xunit; | |
namespace MvcSampleTests | |
{ | |
public class HomeControllerTests | |
{ | |
private HomeController _sut; | |
private HttpContextBase _context; | |
public HomeControllerTests() | |
{ | |
_sut = new HomeController(); | |
_context = Substitute.For<HttpContextBase>(); | |
_sut.ControllerContext = new ControllerContext(_context, new RouteData(), _sut); | |
} | |
[Fact] | |
public void Index_Called_ReturnsView() | |
{ | |
var actionResult = _sut.Index(); | |
actionResult.Should().BeOfType<ViewResult>(); | |
} | |
[Fact] | |
public void Index_Called_IncrementsSessionViewCount() | |
{ | |
_sut.Index(); | |
var x = _context.Received().Session["ViewCount"]; | |
} | |
[Fact] | |
public void Index_ViewCountNull_SetsToOne() | |
{ | |
_context.Session["ViewCount"].Returns(null); | |
_sut.Index(); | |
_context.Session.Received()["ViewCount"] = 1; | |
} | |
[Fact] | |
public void Index_HasValue_Increments() | |
{ | |
_context.Session["ViewCount"].Returns(1); | |
_sut.Index(); | |
_context.Session.Received()["ViewCount"] = 2; | |
} | |
[Fact] | |
public void Index_Count3_Redirect() | |
{ | |
_context.Session["ViewCount"].Returns(3); | |
var result = _sut.Index() as RedirectResult; | |
result.Url.ShouldAllBeEquivalentTo("/home/about"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some notes:
sut
a parameter. But I think this lays out the AAA structure well.