Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking Entity Framework gives me Object reference not set to an instance of an object

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?

like image 285
RamblerToning Avatar asked Oct 23 '25 14:10

RamblerToning


1 Answers

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!

like image 150
Bassam Alugili Avatar answered Oct 26 '25 05:10

Bassam Alugili



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!