Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Last active June 25, 2021 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csharpforevermore/0d0a16f042f707214de293c2c9376688 to your computer and use it in GitHub Desktop.
Save csharpforevermore/0d0a16f042f707214de293c2c9376688 to your computer and use it in GitHub Desktop.
Examples of NUnit and Moq assertions
[SetUp]
public void SetUp()
{
_MockRepository = new MockRepository(MockBehavior.Strict);
_MockLogger = _MockRepository.Create<ILogger<TagController>>();
_MockTagManager = _MockRepository.Create<ITagManager>();
_MockApplicationConfigManager = _MockRepository.Create<IApplicationConfigManager>();
}
private TagController CreateTagController()
{
_MockLogger.Setup(logger => logger.Log(It.IsAny<LogLevel>(),
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => true),
It.IsAny<Exception>(),
It.Is<Func<It.IsAnyType, Exception, string>>((v, t) => true)));
return new TagController(
_MockLogger.Object,
_MockTagManager.Object,
_MockApplicationConfigManager.Object);
}
[Test]
public async Task Edit_check_completed()
{
AsyncTestDelegate act = () => _Controller.Edit(It.IsAny<int>());
Assert.That(act.Invoke().IsCompleted);
}
[Test]
public async Task Edit_check_called_ShowProposalManager()
{
AsyncTestDelegate act = () => _Controller.Edit(It.IsAny<int>());
Assert.That(act.Invoke().IsCompleted);
_ShowProposalManagerMock.Verify(spmm => spmm.GetShowProposalById(It.IsAny<int>()));
}
[Test]
public async Task Check_ViewName()
{
var result = await _Controller.Edit(It.IsAny<int>()) as ViewResult;
Assert.AreEqual(result.ViewName, "~/views/tv/showproposal/edit.cshtml");
}
[Test]
public async Task ShowProposalProductSearchModal_check_throws_ArgumentOutOfRangeException()
{
AsyncTestDelegate act = () => _Controller.ShowProposalProductSearchModal(0);
Assert.That(act, Throws.TypeOf<ArgumentOutOfRangeException>());
}
public class DepartmentController : Controller
{
DepartmentAccess objds;
public DepartmentController()
{
objds = new DepartmentAccess();
}
// GET: Department
public ActionResult Index()
{
var Depts = objds.GetDepartments();
return View("Index",Depts);
}
// GET: Department/Create
public ActionResult Create()
{
var Dept = new Department();
return View(Dept);
}
// POST: Department/Create
[HttpPost]
public ActionResult Create(Department dept)
{
try
{
objds.CreateDepartment(dept);
return RedirectToAction("Index");
}
catch
{
return View(dept);
}
}
}
[TestFixture]
public class ControllerTestClass
{
/// <summary>
/// Test the Action metjod returning Specific Index View
/// </summary>
[Test]
public void TestDepartmentIndex()
{
var obj =new DepartmentController();
var actResult = obj.Index() as ViewResult;
Assert.That(actResult.ViewName, Is.EqualTo("Index"));
}
}
/// <summary>
/// Testing the RedirectToRoute to Check for the Redirect
/// to Index Action
/// </summary>
[Test]
public void TestDepartmentCreateRedirect()
{
var obj = new DepartmentController();
RedirectToRouteResult result = obj.Create(new Department()
{
DeptNo=190,Dname="D1",Location="L1"
}) as RedirectToRouteResult;
Assert.That(result.RouteValues["action"], Is.EqualTo("Index"));
}
/// <summary>
/// Test to return Error View is the Model is Invalid
/// </summary>
[Test]
public void TestDepartmentCreateErrorView()
{
var obj = new DepartmentController();
ViewResult result = obj.Create(new Department()
{
DeptNo = 2,
Dname = "D1",
Location = "L1"
}) as ViewResult;
Assert.That(result.ViewName, Is.EqualTo("Error"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment