4

I'm trying to follow this guide for mocking Entity Framework

https://msdn.microsoft.com/en-us/library/dn314429.aspx

The code from the guide works absolutely fine when I build it in my project, but when I'm trying to apply it to my actual context and data objects, I'm getting an exception:

Object reference not set to an instance of an object

My object is pretty simple:

public class NodeAttributeTitle
{
    public int ID { get; set; }
    [MaxLength(150)]
    public string Title { get; set; }
    public string Description { get; set; }
}

as is my context

public class DataContext : DbContext
{       
    public virtual DbSet<NodeAttributeTitle> NodeAttributeTitles { get; set; }          
}   

and the method I'm trying to set is just a basic insertion

public class CommonNodeAttributes : ICommonNodeAttributes
{
    private DataContext _context;

    public CommonNodeAttributes(DataContext context)
    {
        _context = context;
    }

    public CommonNodeAttributes()
    {
        _context = new DataContext();
    }

    public void Insert(string value)
    {
        var nodeAttributeValue = new NodeAttributeValue();

        nodeAttributeValue.Value = value;
        nodeAttributeValue.Parent = 0;

        _context.NodeAttributeValues.Add(nodeAttributeValue);
        _context.SaveChanges();
    }
}

And the test class is following the same syntax as in the MSDN guide

[TestClass]
public class CommonNodeAttributesTests
{
    [TestMethod]
    public void CreateNodeAttribute_saves_a_nodeattribute_via_context()
    {
        var mockSet = new Mock<DbSet<NodeAttributeTitle>>();
        var mockContext = new Mock<DataContext>();
        mockContext.Setup(m => m.NodeAttributeTitles).Returns(mockSet.Object);
        var service = new CommonNodeAttributes(mockContext.Object);
        service.Insert("blarg");
        mockSet.Verify(m => m.Add(It.IsAny<NodeAttributeTitle>()),Times.Once());
        mockContext.Verify(m => m.SaveChanges(),Times.Once);

    }
}   

and yet when the test runs, I get

Tests.CommonNodeAttributesTests.CreateNodeAttribute_saves_a_nodeattribute_via_context threw exception:

System.NullReferenceException: Object reference not set to an instance of an object.

I don't understand why the code in the guide works fine, but my code doesn't.

I've tried adding 'virtual' to the ID, Title and Description properties but that doesn't do anything either. Does anyone have any clue what might be different for me?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
RamblerToning
  • 926
  • 2
  • 13
  • 28
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Craig W. Jun 07 '16 at 14:04

1 Answers1

3

You need to give some data in your mock:

IQueryable<NodeAttributeTitle> data = new List<NodeAttributeTitle>
{
    new NodeAttributeTitle() {Id = 1, Title = "t1"},
    new NodeAttributeTitle() {Id = 2, Title = "t2"},
}.AsQueryable();
var mockSet = new Mock<IDbSet<NodeAttributeTitle>>();
mockSet .Setup(m => m.Provider).Returns(data.Provider);
mockSet .Setup(m => m.Expression).Returns(data.Expression);
mockSet .Setup(m => m.ElementType).Returns(data.ElementType);
mockSet .Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

Then you can pass it to your DbContext:

Your problem in code is in the Add method ( _context.NodeAttributeValues.Add(nodeAttributeValue);) is not mocked!

Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70