我正在尝试为控制器编写单元测试方法,但是我用测试数据嘲笑的方法不会返回它。
我的控制器:
IDataService _dataService = new DataService();
[HttpPost]
public JsonResult VerifyCredentialsAndGetUserData(string email, string password)
{
return Json(this._dataService.VerifyCustomerAndReturnHisID(email, password), JsonRequestBehavior.AllowGet);
}
其中存在方法VerifyCustomerAndReturnHisID的我的图书馆:
public int VerifyCustomerAndReturnHisID(string email, string password)
{
var emailToCheck = new SqlParameter("@Email", email.ToLowerInvariant());
var pass = new SqlParameter("@Password", password);
var result = dbContext.Customer.SqlQuery("SELECT * FROM Customer WHERE Email = @Email AND Password = @Password", new SqlParameter("@Email", email.ToLowerInvariant()), new SqlParameter("@Password", password)).ToList();
if (result.Count() > 0)
{
return result.FirstOrDefault().ID;
}
else
{
return 0;
}
}
我的测试方法:
[TestMethod]
public void VerifyCredentialsAndGetUserData()
{
var user = new ApplicationDatabase.DB_Models.Customer()
{
Email = "basejohn@test.pl",
Password = "testPass",
ID = 12
};
var mock = new Mock<DbContext>();
var userDbSet = new ApplicationDatabase.DB_Models.FakeDbSet<ApplicationDatabase.DB_Models.Customer>();
userDbSet.Add(user);
this._IdataServiceMock.Setup(x => x.VerifyCustomerAndReturnHisID(It.IsAny<string>(), It.IsAny<string>())).Returns(user.ID);
var a = this._IdataServiceMock.Object.VerifyCustomerAndReturnHisID(user.Email, user.Password); // <-- this will return 12
var result = this.loginPannel.VerifyCredentialsAndGetUserData(user.Email, user.Password) as JsonResult; // <--- This never returns 12
Assert.IsNotNull(result.Data);
Assert.AreEqual(12, result);
}
我希望得到的结果是我模拟的数据是12,请有人帮我,我为此苦苦了很长时间...