11.1.1 Creating a CRUD Controller; UNIT TEST: THE INDEX ACTION
This commit is contained in:
parent
6e6ae67b21
commit
de9d07f75f
41
SportsStore/SportsStore.UnitTests/AdminTests.cs
Normal file
41
SportsStore/SportsStore.UnitTests/AdminTests.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using SportsStore.Domain.Abstract;
|
||||
using SportsStore.Domain.Entities;
|
||||
using SportsStore.WebUI.Controllers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SportsStore.UnitTests
|
||||
{
|
||||
[TestClass]
|
||||
public class AdminTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void Index_Contains_All_Products()
|
||||
{
|
||||
// Arrange - create the mock repository
|
||||
Mock<IProductRepository> mock = new Mock<IProductRepository>();
|
||||
mock.Setup(m => m.Products).Returns(new Product[]
|
||||
{
|
||||
new Product {ProductID = 1, Name = "P1"},
|
||||
new Product {ProductID = 2, Name = "P2"},
|
||||
new Product {ProductID = 3, Name = "P3"},
|
||||
});
|
||||
|
||||
// Arrange - create a controller
|
||||
AdminController target = new AdminController(mock.Object);
|
||||
|
||||
// Action
|
||||
Product[] result = ((IEnumerable<Product>)target.Index().ViewData.Model).ToArray();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(result.Length, 3);
|
||||
Assert.AreEqual("P1", result[0].Name);
|
||||
Assert.AreEqual("P2", result[1].Name);
|
||||
Assert.AreEqual("P3", result[2].Name);
|
||||
}
|
||||
}
|
||||
}
|
@ -87,6 +87,7 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AdminTests.cs" />
|
||||
<Compile Include="App_Start\NinjectWebCommon.cs" />
|
||||
<Compile Include="UnitTest1.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
@ -17,7 +17,7 @@ namespace SportsStore.WebUI.Controllers
|
||||
}
|
||||
|
||||
// GET: Admin
|
||||
public ActionResult Index()
|
||||
public ViewResult Index()
|
||||
{
|
||||
return View(repository.Products);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user