0

I'm trying to use Nunit more and I am a complete noob with it. I have written some tests methods and it seems to think there are null references.

Test class

using LibraryBL.Loan;
using LibraryBLMock.Mock;
using LibraryBLModel.Model;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibraryBLTests
{
    [TestFixture]
    public class LoanManagementTests
    {
        [Test]
        public void CheckReservedFalse() // Passes if reserved = false
        {
            LoanManagement LoanManagement = new LoanManagement();
            MockPublications MockPublications = new LibraryBLMock.Mock.MockPublications();
            List<Publication> l = new List<Publication>();
            l = MockPublications.MockListPublications();
        }

        [Test]
        public void InitCheckOutTest() // Passes if reserved = false
        {
            LoanManagement LoanManagement = new LoanManagement();
            LoanManagement.InitCheckOut("0-330-28498-4");
        }

    }
}

For now I am not using asset method to test any values but for CheckReservedFalse I want to test that my method returns false.

Class methods I want to test

public class LoanManagement
    {
        #region ReservePublication
        public void InitCheckOut(string ISBN)
        {
            if (!CheckReserved(ISBN))
            {

            }
        }

        public bool CheckReserved(string ISBN)
        {
            List<Publication> p = GetPublicationsByISBN("0-330-28498-4");

            if (p.Count == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public List<Publication> GetPublicationsByISBN(string ISBN)
        {
             LibraryBLMock.Mock.MockPublications m = new LibraryBLMock.Mock.MockPublications(); // Use MOCK instead of data layer
             return m.MockListPublications()
                .Where(i => i.ISBN == ISBN && i.reserved == false).ToList();

        }

        public void SetPublication(string Status)
        {
            switch(Status)
            {
                case "CheckOut":
                    // DB method to set book as reserved
                    break;
            }
        }

        #endregion

        public void CheckOutPublication()
        {
            string s = "";
            if(CheckReserved("0-330-28498-4"))
            {
                s = "This publication is not available at this current time";
            }
            else
            {
                //s = "There are" + p.Count + " of" + p.FirstOrDefault().Title + " available for loan";
            }
        }

Exception screen

So I want to know how I test the values that my objects return. Why am I get a null reference when in my code it compiles and I am instantiating those classes.

Apologies if my question is a bit dumb.

nick gowdy
  • 6,191
  • 25
  • 88
  • 157
  • (As a side note, your `LoanManagement` class shouldn't know about mocking at all. You should be injecting a dependency so that the test can say that *it* wants a mock, without affecting production behaviour.) – Jon Skeet Jul 08 '14 at 12:15
  • @JonSkeet Hi Jon, I've been given the task of creating a middle tier and I want to write some tests. My question is for help with nUnit and testing my methods. They fail because of null reference. – nick gowdy Jul 08 '14 at 13:10
  • Tests normally have an "Assert" - I do not see any Assert in your code. Or do you just test that no Exceptions occur? Beyond the erroneous use of Mocking in LoanManagement, GetPublicationsByISBN should be the first method to be tested, both with positive and negative examples. In CheckReserved, you use a literal as parameter to GetPublicationsByISBN - that's surely wrong. How can you test InitCheckOut - just that it does not throw an exception? Or by "side effects" like member variables being changed? – Bernhard Hiller Jul 08 '14 at 13:14
  • So you need to work out where the exception is occurring, and why. That's basic diagnostics, and doesn't have that much to do with nunit... – Jon Skeet Jul 08 '14 at 13:23
  • @BernhardHiller I've said that in my question I am a bit of a noob when it comes to unit testing. My methods are dependant on instantiating an object and calling it's method. I am not being facetious, I want to mock a database call and test it's success on either a count on rows or a boolean being returned. – nick gowdy Jul 08 '14 at 13:25
  • p1.Author.Name = "John Doe"; - p1.Author is null, isn't it? Also: do not forget to add the "publication" objects to the List; be careful to use the correct variable names, etc... – Bernhard Hiller Jul 08 '14 at 13:54

0 Answers0