Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 模拟实体框架为我提供了未设置为对象实例的对象引用_C#_Entity Framework_Unit Testing - Fatal编程技术网

C# 模拟实体框架为我提供了未设置为对象实例的对象引用

C# 模拟实体框架为我提供了未设置为对象实例的对象引用,c#,entity-framework,unit-testing,C#,Entity Framework,Unit Testing,我试着按照这个指南来模拟实体框架 当我在我的项目中构建指南中的代码时,它工作得非常好,但是当我试图将其应用于实际的上下文和数据对象时,我遇到了一个异常: 对象引用未设置为对象的实例 我的目标很简单: public class NodeAttributeTitle { public int ID { get; set; } [MaxLength(150)] public string Title { get; set; } public string Descri

我试着按照这个指南来模拟实体框架

当我在我的项目中构建指南中的代码时,它工作得非常好,但是当我试图将其应用于实际的上下文和数据对象时,我遇到了一个异常:

对象引用未设置为对象的实例

我的目标很简单:

public class NodeAttributeTitle
{
    public int ID { get; set; }
    [MaxLength(150)]
    public string Title { get; set; }
    public string Description { get; set; }
}
这是我的背景

public class DataContext : DbContext
{       
    public virtual DbSet<NodeAttributeTitle> NodeAttributeTitles { get; set; }          
}   
测试类遵循与MSDN指南中相同的语法

[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);

    }
}   
[TestClass]
公共类CommonNodeAttributesTesters
{
[测试方法]
public void CreateNodeAttribute_通过_context()保存_nodeattribute_
{
var mockSet=new Mock();
var mockContext=new Mock();
Setup(m=>m.NodeAttributeTitles).Returns(mockSet.Object);
var服务=新的CommonNodeAttributes(mockContext.Object);
服务。插入(“blarg”);
验证(m=>m.Add(It.IsAny()),Times.Once());
验证(m=>m.SaveChanges(),Times.Once);
}
}   
然而,当测试运行时,我得到

Tests.CommonNodeAttributeTests.CreateNodeAttribute\u通过\u上下文保存\u nodeattribute\u引发异常:

System.NullReferenceException:对象引用未设置为对象的实例

我不明白为什么指南中的代码可以正常工作,但我的代码不行


我尝试过在ID、Title和Description属性中添加“virtual”,但这也没有任何作用。有人知道我会有什么不同吗

您需要在模拟中提供一些数据:

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());
IQueryable数据=新列表
{
新的NodeAttributeTitle(){Id=1,Title=“t1”},
新的NodeAttributeTitle(){Id=2,Title=“t2”},
}.AsQueryable();
var mockSet=new Mock();
Setup(m=>m.Provider).Returns(data.Provider);
Setup(m=>m.Expression).Returns(data.Expression);
Setup(m=>m.ElementType).Returns(data.ElementType);
Setup(m=>m.GetEnumerator())。返回(data.GetEnumerator());
然后,您可以将其传递到DbContext:

代码中的问题在于Add方法(\u context.NodeAttributeValues.Add(NodeAttributeValues);)未被模拟

可能重复的
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());