gpt4 book ai didi

c# - 系统.ArgumentNullException : Value cannot be null

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:17 25 4
gpt4 key购买 nike

我正在为我的 CRUD 创建单元测试并在我的 Delete 测试方法中收到此类错误:

System.ArgumentNullException: Value cannot be null.

我检查后发现,每当我运行测试时,我的用户 ID 下的内容已被删除,但在我的测试结果栏中收到“失败”状态。谁能帮我解决我的问题?

Error Message: Test method TestProject1.UserTest.Delete threw exception: 
System.ArgumentNullException: Value cannot be null.
Parameter name: entity

这是我的代码:

[TestMethod]
public void Delete()
{
taskModelContainer db = new taskModelContainer();

int userID = 7;

TaskMgr.Models.User UserEntity = new TaskMgr.Models.User();

UserEntity.userID = userID;

UserController User = new UserController();

ActionResult result = User.DeleteConfirmed(UserEntity.userID);
int id = Convert.ToInt32(User.ViewBag.id);
User Users = db.Users.Single(e => e.userID == id);


Assert.IsNull(Users.userID);

}

Controller :

[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
User user = db.Users.Find(id);
db.Users.Remove(user);
db.SaveChanges();
var qry = from e in db.Users orderby e.userID descending select e.userID;
int eID = qry.First();
ViewBag.id = user.userID;
return RedirectToAction("Index");
}

它在 Controller 的这一行停止:db.Users.Remove(user);

最佳答案

我通过这样做解决了我的问题:

[TestMethod]
public void Delete()
{
taskModelContainer db = new taskModelContainer();

int userID = 41;

TaskMgr.Models.User UserEntity = new TaskMgr.Models.User();

UserEntity.userID = userID;

UserController User = new UserController();

ActionResult result = User.DeleteConfirmed(UserEntity.userID);

var del = from e in db.Users where e.userID == UserEntity.userID select e;
var delete = del.Count();

Assert.AreEqual(0, delete);

}

我查询并检查了用户 ID 及其内容是否被删除。我没有更改我的 Controller 中的任何内容。

关于c# - 系统.ArgumentNullException : Value cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12792378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com