我不明白为什么它说我试图将int转换为bool。这可能是一个简单的问题,但它超出了我的想象。有人能看到我的问题吗。我在"if(dao.Update(call))"one_answers"if(vm.Update())"的单元测试中遇到错误。它说我不能隐式地将int转换为bool。它可能是我的DAO和ViewModel的Update方法吗?提前感谢任何能帮助我的人。干杯
这是我的实体类:
public class Call : IMongoEntity
{
public ObjectId _id { get; set; }
public string _accessId { get; set; }
public ObjectId EmployeeId { get; set; }
public ObjectId ProblemId { get; set; }
public ObjectId TechId { get; set; }
public System.DateTime DateOpened { get; set; }
public System.DateTime? DateClosed { get; set; }
public bool OpenStatus { get; set; }
public string Notes { get; set; }
}
这是我的DAO代码:
public int Update(Call call)
{
int updateOK = -1;
try
{
DbContext ctx = new DbContext();
ctx.Save<Call>(call, "calls");
updateOK = 1;
}
catch (MongoConcurrencyException ex)
{
updateOK = -2;
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Problem " + ex.Message);
DALUtils.ErrorRoutine(ex, "CallDAO", "Update");
}
return updateOK;
}
这是我的ViewModel代码:
public int Update()
{
int rowsUpdated = -1;
try
{
Call call = new Call();
call.DateOpened = DateOpened;
call.DateClosed = DateClosed;
call.OpenStatus = OpenStatus;
call.Notes = Notes;
rowsUpdated = _dao.Update(call);
}
catch (Exception ex)
{
ViewModelsUitls.ErrorRoutine(ex, "CallViewModel", "Update");
}
return rowsUpdated;
}
这是我为DAO和ViewModel编写的单元测试代码:
[TestMethod]
public void CallDAOComprehensiveTestsReturnTrue()
{
CallDAO dao = new CallDAO();
Call call = new Call();
call.DateOpened = DateTime.Now;
call.DateClosed = null;
call.OpenStatus = true;
call.EmployeeId = new MongoDB.Bson.ObjectId("56527d3e3e400c1adc33ad37"); // bigshot
call.TechId = new MongoDB.Bson.ObjectId("56527d3e3e400c1adc33ad3d"); // Burner
call.ProblemId = new MongoDB.Bson.ObjectId("56527d3e3e400c1adc33ad44");//memory
call.Notes = "Bigshot has bad RAM, Burner to fix it";
string newId = dao.Create(call);
this.testContextInstance.WriteLine("New Call Id == " + newId);
call = dao.GetById(newId);
this.testContextInstance.WriteLine("Call retrieved");
call.Notes = call.Notes + "nOrdered new Ram";
if (dao.Update(call))
this.testContextInstance.WriteLine("Call was updated " + call.Notes);
else
Trace.WriteLine("Call was not updated");
if (dao.Delete(newId))
this.testContextInstance.WriteLine("call was deleted");
else
this.testContextInstance.WriteLine("call was not deleted");
call = dao.GetById(newId);
Assert.IsNull(call);
}
[TestMethod]
[ExpectedException(typeof(MongoDB.Driver.MongoException),"No Id exists")]
public void CallViewModelComprehensiveTestsReturnTrue()
{
CallViewModel vm = new CallViewModel();
vm.DateOpened = DateTime.Now;
vm.OpenStatus = true;
vm.EmployeeId = "56527d3e3e400c1adc33ad37"; // Bigshot
vm.TechId = "56527d3e3e400c1adc33ad3d"; // Burner
vm.ProblemId = "56527d3e3e400c1adc33ad44"; // memory
vm.Notes = "Bigshot has ban RAM, Burner to fix it";
vm.Create();
this.testContextInstance.WriteLine("New call Id == " + vm.Id);
vm.GetById(vm.Id);
this.testContextInstance.WriteLine("Call retrieved");
vm.Notes = vm.Notes + "nOrdered new Ram";
if (vm.Update())
this.testContextInstance.WriteLine("Call was updated " + vm.Notes);
else
Trace.WriteLine("Call was not updated");
if (vm.Delete(vm.Id))
this.testContextInstance.WriteLine("Call was deleted");
else
this.testContextInstance.WriteLine("Call was not deleted");
vm.Update(); // should throw MongoException see attribute
}
dao.Update(call)
返回一个整数。整数不是布尔值。把它放在if语句中就相当于做
if (dao.Update(call) == true)
翻译成
if ([integer value] == true)
这就是你看到错误的原因。
您需要做的是将Update()
的返回类型更改为布尔值,或者检查返回的整数是否为期望值,例如
if (dao.Update(call) > 0)