Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
Created July 27, 2014 04:52
Show Gist options
  • Save AlexArchive/6f60615a1f893b82868c to your computer and use it in GitHub Desktop.
Save AlexArchive/6f60615a1f893b82868c to your computer and use it in GitHub Desktop.
using GoBlog.AutoMapper;
using GoBlog.Controllers;
using GoBlog.Data;
using GoBlog.Data.Entities;
using GoBlog.Models;
using GoBlog.Paging;
using Moq;
using NUnit.Framework;
using System;
using System.Collections.ObjectModel;
using System.Web.Mvc;
namespace GoBlog.Test
{
[TestFixture]
public class PostsControllerTest
{
private PostsController controller;
[SetUp]
public void SetUp()
{
AutoMapperConfig.Configure();
var repository = new Mock<IPostsRepository>();
controller = new PostsController(repository.Object);
}
[Test]
public void Index_ReturnsCorrectViewName()
{
var actual = controller.Index();
Assert.AreEqual(actual.ViewName, "Index");
}
[Test]
public void Index_ReturnsCorrectModel()
{
var result = controller.Index();
var actual = result.Model as PagedList<PostViewModel>;
Assert.NotNull(actual);
}
[Test]
public void Index_WithPageNumber_ReturnsCorrectViewName()
{
var actual = controller.Index(2);
Assert.AreEqual(actual.ViewName, "Index");
}
[Test]
public void Index_WithPageNumber_ReturnsCorrectModel()
{
var result = controller.Index(2);
var actual = result.Model as PagedList<PostViewModel>;
Assert.NotNull(actual);
}
[Test]
public void Post_ReturnsCorrectViewName()
{
var repository = new Mock<IPostsRepository>();
repository.Setup(expression => expression.Find("arbitrary-post")).Returns(new Post());
var sut = new PostsController(repository.Object);
var actual = sut.Post("arbitrary-post") as ViewResult;
Assert.NotNull(actual);
Assert.AreEqual(actual.ViewName, "Post");
}
[Test]
public void Post_ThatIsDraft_ReturnsNotFound()
{
var repository = new Mock<IPostsRepository>();
repository.Setup(expression => expression.Find("arbitrary-post")).Returns(new Post{Draft=true});
var sut = new PostsController(repository.Object);
var actual = sut.Post("arbitrary-post") ;
Assert.IsAssignableFrom<HttpNotFoundResult>(actual);
}
[Test]
public void Post_ThatDoesNotExist_ReturnNotFound()
{
var actual = controller.Post("non-existent-post");
Assert.IsAssignableFrom<HttpNotFoundResult>(actual);
}
[Test]
public void Post_ReturnsCorrectModel()
{
var repository = new Mock<IPostsRepository>();
var expected = new Post
{
Slug = "continuing-to-an-outer-loop",
Title = "Continuing to an outer loop",
Summary = "When you have a nested loop, sometimes",
Content = "When you have a nested loop, sometimes",
PublishedAt = DateTime.Now.AddDays(7),
Tags = new Collection<Tag> {new Tag {Name = "Programming"}}
};
repository.Setup(expression => expression.Find("arbitrary-post")).Returns(expected);
var sut = new PostsController(repository.Object);
var result = (ViewResult) sut.Post("arbitrary-post");
var actual = result.Model as PostViewModel;
Assert.NotNull(actual);
Assert.AreEqual(actual.Slug, expected.Slug);
Assert.AreEqual(actual.Title, expected.Title);
Assert.AreEqual(actual.Summary, expected.Summary);
Assert.AreEqual(actual.Content, expected.Content);
Assert.AreEqual(actual.PublishedAt, expected.PublishedAt);
Assert.AreEqual(actual.Tags, expected.Tags);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment