7 Replies - 5016 Views - Last Post: 20 June 2015 - 09:58 AM Rate Topic: -----

#1 [email protected]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-June 15

Unit Testing Failing C#: Not instance of an object

Posted 20 June 2015 - 05:05 AM

Hi Everybody,

I'm new here and new to C# also. I'm trying to learn OOP with C# and I'm currently doing the Pluralsight course: Object-Oriented Programming Fundamentals in C#. I'm getting on well with it, but I've been completely stuck on an error for the last couple of days that I keep getting when running two of the unit tests in my project (the rest of the tests have passed ok).

Basically the course walks you through building an Ordering System which Customers can use to order different products. The unit test which produces the error is the "CustomerRepositoryTest" which has two methods: one for testing to make sure the Customer Repository returns a Customer when a specific id is passed in and one that checks whether the Customer returns with a list of addresses.

The values are hard coded into the Customer and Customer Repository classes so that there is no need to set up a database for the purpose of the course.

The error I keep getting is this:

ACMBL.TEST.CustomerRepositoryTest.RetrieveExistingWithAddress threw Exception. System.NullReferenceException: Object not set to instance of an object

I've checked through the completed course files but my code seems to be the same as them, so I'm a bit lost as to why the tests keep failing. Here's the code for the CustomerRepositoryTest file:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ACM___Business_Layer;
using System.Collections.Generic;

namespace ACMBL.TEST
{
    [TestClass]
    public class CustomerRepositoryTest
    {
        [TestMethod]
        public void RetrieveExisting()
        {
            //-- Arrange
            var customerRepository = new Customer_Repository();
            var expected = new Customer(1)
            {
                EmailAddress = "[email protected]",
                FirstName = "Frodo",
                LastName = "Baggins"
            };

            //-- Act
            var actual = customerRepository.Retrieve(1);

            //-- Assert
            //Assert.AreEqual(expected, actual);

            Assert.AreEqual(expected.CustomerId, actual.CustomerId);
            Assert.AreEqual(expected.EmailAddress, actual.EmailAddress);
            Assert.AreEqual(expected.FirstName, actual.FirstName);
            Assert.AreEqual(expected.LastName, actual.LastName);
        }

        [TestMethod]
        public void RetrieveExistingWithAddress()
        {
            //-- Arrange
            var customerRepository = new Customer_Repository();
            var expected = new Customer(1)
            {
                EmailAddress = "[email protected]",
                FirstName = "Frodo",
                LastName = "Baggins",
                AddressList = new List<Address>()
                    {
                        new Address()
                        {
                            AddressType = 1,
                            StreetLine1 = "Bag End",
                            StreetLine2 = "Bagshot row",
                            City = "Hobbiton",
                            State = "Shire",
                            Country = "Middle Earth",
                            PostalCode = "144"
                        },
                        new Address()
                        {
                            AddressType = 2,
                            StreetLine1 = "Green Dragon",
                            City = "Bywater",
                            State = "Shire",
                            Country = "Middle Earth",
                            PostalCode = "146"
                        }
                    }
            };


            //-- Act
            var actual = customerRepository.Retrieve(1);

            //-- Assert
            Assert.AreEqual(expected.CustomerId, actual.CustomerId);
            Assert.AreEqual(expected.EmailAddress, actual.EmailAddress);
            Assert.AreEqual(expected.FirstName, actual.FirstName);
            Assert.AreEqual(expected.LastName, actual.LastName);

            for (int i = 0; i < 1; i++)
            {
                Assert.AreEqual(expected.AddressList[i].AddressType, actual.AddressList[i].AddressType);
                Assert.AreEqual(expected.AddressList[i].StreetLine1, actual.AddressList[i].StreetLine1);
                Assert.AreEqual(expected.AddressList[i].City, actual.AddressList[i].City);
                Assert.AreEqual(expected.AddressList[i].State, actual.AddressList[i].State);
                Assert.AreEqual(expected.AddressList[i].Country, actual.AddressList[i].Country);
                Assert.AreEqual(expected.AddressList[i].PostalCode, actual.AddressList[i].PostalCode);
            }

        }
    }
}





Any help on this would be much appreciated. Let me know if you need any further information, something tells me I haven't explained it enough lol.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Unit Testing Failing C#: Not instance of an object

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Unit Testing Failing C#: Not instance of an object

Posted 20 June 2015 - 05:11 AM

What line creates the error?
Was This Post Helpful? 0
  • +
  • -

#3 [email protected]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-June 15

Re: Unit Testing Failing C#: Not instance of an object

Posted 20 June 2015 - 05:35 AM

Line 13 and Line 37, both methods seem to fail
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Unit Testing Failing C#: Not instance of an object

Posted 20 June 2015 - 05:44 AM

Those lines just have opening braces..

Post the full error details.
Was This Post Helpful? 0
  • +
  • -

#5 [email protected]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-June 15

Re: Unit Testing Failing C#: Not instance of an object

Posted 20 June 2015 - 05:58 AM

I've attached a screenshot of the details its giving me in test explorer as to why the tests fail.

I also have this in the output section if this is any help:

1> ACMBL -> F:\Pluralsight - Object-Oriented Programming Fundamentals in C#\my_code\ACM\ACM - Business Layer\bin\Debug\ACM - Business Layer.dll
2>------ Build started: Project: ACMBL.TEST, Configuration: Debug Any CPU ------
2>Project file contains ToolsVersion="12.0". This toolset may be unknown or missing, in which case you may be able to resolve this by installing the appropriate version of MSBuild, or the build may have been forced to a particular ToolsVersion for policy reasons. Treating the project as if it had ToolsVersion="4.0". For more information, please see http://go.microsoft....?LinkId=291333.
2>Project file contains ToolsVersion="12.0". This toolset may be unknown or missing, in which case you may be able to resolve this by installing the appropriate version of MSBuild, or the build may have been forced to a particular ToolsVersion for policy reasons. Treating the project as if it had ToolsVersion="4.0". For more information, please see http://go.microsoft....?LinkId=291333.
2>Project file contains ToolsVersion="12.0". This toolset may be unknown or missing, in which case you may be able to resolve this by installing the appropriate version of MSBuild, or the build may have been forced to a particular ToolsVersion for policy reasons. Treating the project as if it had ToolsVersion="4.0". For more information, please see http://go.microsoft....?LinkId=291333.
2>Project file contains ToolsVersion="12.0". This toolset may be unknown or missing, in which case you may be able to resolve this by installing the appropriate version of MSBuild, or the build may have been forced to a particular ToolsVersion for policy reasons. Treating the project as if it had ToolsVersion="4.0". For more information, please see http://go.microsoft....?LinkId=291333.
2> ACMBL.TEST -> F:\Pluralsight - Object-Oriented Programming Fundamentals in C#\my_code\ACM\ACMBL.TEST\bin\Debug\ACMBL.TEST.dll
========== Build: 2 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Unit Testing Failing C#: Not instance of an object

Posted 20 June 2015 - 06:55 AM

Just a quick look so I may be missing something but...

You make customer with no address:
16	            var expected = new Customer(1)
17	            {
18	                EmailAddress = "[email protected]",
19	                FirstName = "Frodo",
20	                LastName = "Baggins"
21	            };


And you are comparing against expections that do have an address:
79	            for (int i = 0; i < 1; i++)
80	            {
81	                Assert.AreEqual(expected.AddressList[i].AddressType, actual.AddressList[i].AddressType);
82	                Assert.AreEqual(expected.AddressList[i].StreetLine1, actual.AddressList[i].StreetLine1);
83	                Assert.AreEqual(expected.AddressList[i].City, actual.AddressList[i].City);
84	                Assert.AreEqual(expected.AddressList[i].State, actual.AddressList[i].State);
85	                Assert.AreEqual(expected.AddressList[i].Country, actual.AddressList[i].Country);
86	                Assert.AreEqual(expected.AddressList[i].PostalCode, actual.AddressList[i].PostalCode);
87	            }


So yea, seems like it would be null. It also seems like a screwy loop. Whats the point of a loop that only runs once? Is there a plan later to have this run multiple times? If so that would be a huge assumption that both objects have the same number of addresses, wouldn't it? I mean you're doing the check because you realize there might be a condition where one object doesn't match the other... but there is no checking if anything is null or if the .Count() of the two AddressLists match. Its seems almost inevitable that an exception would be thrown here at some point.

This post has been edited by tlhIn`toq: 20 June 2015 - 06:56 AM

Was This Post Helpful? 0
  • +
  • -

#7 [email protected]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-June 15

Re: Unit Testing Failing C#: Not instance of an object

Posted 20 June 2015 - 09:22 AM

Thanks for the reply, I see what you mean about the Address but I only create an address and use the for loop in the RetrieveExistingWithAddress() method when I do create addresses and not with the Retrieve() class. The customer class creates an empty Address List on instantiation like so:

        public Customer()
            :this(0)
        {
                
        }

        public Customer(int customerId)
        {
            this.CustomerId = customerId;  
 
            AddressList = new List<Address>();
        }




There is a lot of different relationships between the classes also which makes it hard to put my finger on the problem.

I know what you mean about the for loop, as I say its part of a online course (which i'm starting to increasingly dislike).

This post has been edited by andrewsw: 20 June 2015 - 09:59 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Unit Testing Failing C#: Not instance of an object

Posted 20 June 2015 - 09:58 AM

But a new AddressList is empty. If you try to retrieve element [i] from it you'll throw errors.

If you don't call the method except for very specific instances where you already know the conditions won't throw errors then okay. I just saw the two and could see the potential for a problem - but you have the project to actually run/debug and step through.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1