MOQ C# QUERIES It.IsAny returning a List

送分小仙女□ 提交于 2020-01-25 03:53:17

问题


I have made a lot of progress during the night. I finally managed to set up my tests. Now my code will execute the first test and end up in the CREATE METHOD of the controller. What I am trying to see if I need to change my controller method so it can take a full object (perhaps create a new IF statement checking if the admin KEY and Admin Name are not null. Do yo have any suggestions as to the design pattern and realistic Unit Testing design?

Test:

public void Creating_One_Note()
    {

        var note = new AdminNote()
        {
            NoteId = 00003,
            UserKey = "89df3f2a-0c65-4552-906a-08bceabb1198",
            AdminKey = "4b942342-8f73-490c-b9df-f29ac859d7d7",
            NoteText = "TEST NOTE FOR THIS TEST YOU KNOW",
            CreateDate = DateTime.Now,
            ModifiedDate = DateTime.Now,
            AdminName = "Marco",
        };
        var a = _controller.Create(note);

        Assert.IsNotNull(a);
    }

Controller Create Method:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(AdminNote adminNote)
    {
        try
        {
            if (ModelState.IsValid)
            {
                adminNote.AdminKey = System.Web.HttpContext.Current.User.Identity.GetUserId();
                adminNote.AdminName = System.Web.HttpContext.Current.User.Identity.GetUserName();
                adminNote.CreateDate = DateTime.Now;
                adminNote.ModifiedDate = DateTime.Now;

                adminNote.ObjectState = ObjectState.Added;
                _adminNoteService.Insert(adminNote);



                return RedirectToAction("UserDetails", "Admin", new { UserKey = adminNote.UserKey });
            }
        }
        catch (Exception ex)
        {
            ControllerConstants.HandleException(ex);
            ViewBag.PopupMessage(string.Format("We're sorry but an error occurred. {0}", ex.Message));
        }

        return View(adminNote);
    }

As you can see my controller method returns a VIEW I know I will have to adjust my test to be able to understand this. However the controller method will jump to the exception because its trying to set the AdminKey and AdminName (information I get from the front end). For this test I am hardcoding it in and I want to see if the method adds it to the repository. What could I do in this case?


回答1:


_adminNoteRepository.Setup(r => r.Find(It.IsAny<AdminNote>())).Returns<List<AdminNote>>(null);

This is returning null because you told it to :). Moq returns the object that you pass as a parameter to the Returns method. The type argument for the Returns method (List<AdminNote> in your case) just specifies what the type of the returned value is, but Moq will not automatically create an instance of that type.

If you want Moq to return a non-null value, then you should pass a non-null value, for example:

_adminNoteRepository.Setup(r => r.Find(It.IsAny<AdminNote>()))
    .Returns<List<AdminNote>>(new List<AdminNote>());

Additionally, in this case the compiler can implicitly determine the the type argument for the Returns method, so you can omit it and simply write:

_adminNoteRepository.Setup(r => r.Find(It.IsAny<AdminNote>()))
    .Returns(new List<AdminNote>());


来源:https://stackoverflow.com/questions/30767162/moq-c-sharp-queries-it-isany-returning-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!