-
Notifications
You must be signed in to change notification settings - Fork 2
/
HomeControllerTest.cs
99 lines (77 loc) · 3.18 KB
/
HomeControllerTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
namespace WebNoteAOP.Tests
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using NUnit.Framework;
using WebNoteAOP.Controllers;
using WebNoteAOP.Models;
using WebNoteAOP.Models.WebNote;
using Assert = NUnit.Framework.Assert;
[TestClass]
public class HomeControllerTest
{
[TestMethod]
public void IndexWasChangedTest()
{
// Arrange Repository
Mock<IWebNoteService> service = new Mock<IWebNoteService>();
service.SetupAllProperties();
// Arrange Controller
HomeController controller = new HomeController { WebNoteService = service.Object };
// Act
ViewResult result = controller.Index() as ViewResult;
string message = result.ViewData["Message"] as string;
// Assert
Assert.AreNotEqual("Welcome to ASP.NET MVC!", message);
}
[TestMethod]
public void IndexTest()
{
// Arrange
var expectedList = this.CreateNotesList();
// Arrange Repository
Mock<IWebNoteService> service = new Mock<IWebNoteService>();
service.Setup(r => r.ReadAll()).Returns(expectedList);
// Arrange Controller
HomeController controller = new HomeController { WebNoteService = service.Object };
// Act
ViewResult result = controller.Index() as ViewResult;
ViewDataDictionary viewData = result.ViewData;
var returnedList = viewData.Model as List<NoteWithCategories>;
// Assert
Assert.That(returnedList, Is.EquivalentTo(expectedList));
}
[TestMethod]
public void IndexShoudlUseRepositoryTest()
{
// Arrange
Mock<IWebNoteService> service = new Mock<IWebNoteService>();
HomeController controller = new HomeController { WebNoteService = service.Object };
// Act
controller.Index();
// Assert - method called at least once
service.Verify(r => r.ReadAll(), Times.AtLeastOnce());
}
/// <summary>
/// Returns a collection for the MockObjectSet
/// </summary>
/// <returns>three Notes</returns>
public List<NoteWithCategories> CreateNotesList()
{
DateTime friday = DateTime.Parse("2010-10-29", CultureInfo.InvariantCulture);
var notes = new List<Note>
{
new Note { NoteId = 1, Title = "Hello", Message = "World", Added = friday },
new Note { NoteId = 2, Title = "Foo", Message = "Bar", Added = friday },
new Note { NoteId = 3, Title = "Unit", Message = "Test", Added = friday }
};
List<NoteWithCategories> extendedNotes = notes.Select(note => new NoteWithCategories(note, null)).ToList();
return extendedNotes;
}
}
}