我想测试我的代码是否正常工作,所以我正在为它编写一些单元测试。
我想检查何时和我的列表中具有相同值的项目再次添加它应该返回false。
我真的不明白如何设置它。我希望有些人可以帮助我。 在我的代码下面
GuestResponseRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PartyInvites.Abstract;
namespace PartyInvites.Models
{
public class GuestResponseRepository : IRepository
{
private static List<GuestResponse> responses = new List<GuestResponse>();
IEnumerable<GuestResponse> IRepository.GetAllResponses()
{
return responses;
}
bool IRepository.AddResponse(GuestResponse response)
{
if (responses.Any(x => x.Email == response.Email)) //here
{
if (responses.Any(x => x.WillAttend == response.WillAttend)) //here
{
return false;
}
var attend = responses.First(x => x.Email == response.Email && x.WillAttend != response.WillAttend);
attend.WillAttend = response.WillAttend;
return true;
}
responses.Add(response);
return true;
}
}
}
我的界面与GuestResponseRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PartyInvites.Models;
namespace PartyInvites.Abstract
{
public interface IRepository
{
IEnumerable<GuestResponse> GetAllResponses();
bool AddResponse(GuestResponse response);
}
}
我家用控制器的一部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartyInvites.Models;
using PartyInvites.Abstract;
namespace PartyInvites.Controllers {
public class HomeController : Controller {
private IRepository repository;
public HomeController(IRepository iRepository)
{
this.repository = iRepository;
}
[HttpGet]
public ViewResult RsvpForm() {
return View();
}
[HttpPost]
public ViewResult RsvpForm(GuestResponse guestResponse) {
if (ModelState.IsValid) {
bool result = repository.AddResponse(guestResponse);
ViewBag.Response = result;
repository.AddResponse(guestResponse);
return View("Thanks", guestResponse);
}
else
{
// there is a validation error
return View();
}
}
}
}
我的UnitTest到目前为止我得到了什么
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PartyInvites.Abstract;
using PartyInvites.Controllers;
using PartyInvites.Models;
using Moq;
namespace Aanmelden
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void FirstResponse()
{
//Arange
Mock<IRepository> mock = new Mock<IRepository>();
mock.Setup(m => m.AddResponse()).Returns(new GuestResponse[] {
new GuestResponse {Name = "Valheru", Email = "valheru@hotmail.com", Phone = "12345678", WillAttend = true}
});
HomeController controller = new HomeController(mock.Object);
//Act
new GuestResponse
{
Name = "Valheru",
Email = "valheru@hotmail.com",
Phone = "12345678",
WillAttend = true
};
//Assert
if viewbag.response = result == false test succeeded
}
}
}
出于某种原因mock.Setup(m => m.**AddResponse**()).Returns(new GuestResponse[] {...
“AddResponse”是正确的说Visual Studio - &gt;
没有任何论据符合所要求的形式 IRepository.AddResponse(GuestReponse)的参数'response'
我也不确定我的Act代码,我完全失去了Assert部分。我希望有人可以帮助我!
答案 0 :(得分:0)
您应该It.IsAny<GuestResponse>()
作为参数,以匹配您的界面的签名
Mock<IRepository> mock = new Mock<IRepository>();
mock.Setup(m => m.AddResponse(It.IsAny<GuestResponse>())).Returns(true);
答案 1 :(得分:0)
基于
我想检查列表中具有相同值的项目何时添加 再次它应该返回false。
您实际上应该测试GuestResponseRepository
实现本身,而不是控制器。
[TestMethod]
public void Duplicate_Added_Should_Return_False {
//Arrange
var sut = new GuestResponseRepository();
var model = new GuestResponse
{
Name = "Valheru",
Email = "valheru@hotmail.com",
Phone = "12345678",
WillAttend = true
};
sut.AddResponse(model);
var expected = false;
//Act
var actual = sut.AddResponse(model);
//Assert
Assert.AreEqual(expected, actual);
}