MOQ C# QUERIES It.IsAny returning a List

1.2k views Asked by At

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

There are 1 answers

0
AJ Richardson On
_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>());