namespace TestApp.Tests
{
[TestClass]
public class TestSampleItemController
{
[TestMethod]
public void GetAllItem_ShouldReturnAllItems()
{
var testItems = GetTestItems();
var controller = new SampleItemController(testItems);
var result = controller.GetAllItems() as List<Item>;
Assert.AreEqual(testItems.Count, result.Count);
}
[TestMethod]
public void GetItem_ShouldReturnCorrectItem()
{
var testItems = GetTestItems();
var controller = new SampleItemController(testItems);
var result = controller.GetItem(4) as OkNegotiatedContentResult<Item>;
Assert.IsNotNull(result);
Assert.AreEqual(testItems[3].Name, result.Content.Name);
}
[TestMethod]
public void GetItem_ShouldNotFindItem()
{
var controller = new SampleItemController(GetTestItems());
var result = controller.GetItem(999);
Assert.IsInstanceOfType(result, typeof(NotFoundResult));
}
private List<Item> GetTestItems()
{
var testItems = new List<Item>();
testItems.Add(new Item{ ID = 1, Name = "Test1", Cost = 1 });
testItems.Add(new Item { ID = 2, Name = "Test2", Cost = 3.75M });
testItems.Add(new Item { ID = 3, Name = "Test3", Cost = 16.99M });
testItems.Add(new Item { ID = 4, Name = "Test4", Cost = 11.00M });
return testItems;
}
}
}