简体   繁体   English

Moq无法解析ICollection参考

[英]Moq not resolving ICollection reference

I am using Moq.4.2.1507.0118 and I'm having a problem that I have not seen before. 我正在使用Moq.4.2.1507.0118,但遇到了一个我从未见过的问题。 The situation is that I have two classes in my model (A and B) with a one to many relationship. 情况是我的模型中有两个类(一对和一对一)。 For example: 例如:

[Table("A")]
public partial class A
{

    public A()
    {
        this.theB = new HashSet<B>();
    }

    [Key]
    public int AId{ get; set; }
    public string Name{ get; set; }

    public virtual ICollection<B> theB{ get; set; }

}

[Table("B")]
public partial class B
{
    [Key]
    public int BId{ get; set; }
    public int AId{ get; set; }
    public virtual A theA { get; set; }
}

Of course I also have the DbSet's defined in the DbContext. 当然,我在DbContext中也定义了DbSet。

Then, in my test code, I have: 然后,在我的测试代码中,我有:

    [TestMethod]
    public void SomeTest()
    {
        var theAs = new List<A>
        {
            new A{ AId=1, Name="test" }
        };
        var theBs= new List<B>
        {
            new B{ BId=1, AId=1, theA=theAs[0] }
        };

        var ASet= new Mock<DbSet<A>>().SetupData(theAs);
        var BSet= new Mock<DbSet<B>>().SetupData(theBs);

        var context = new Mock<MyContext>();
        context.Setup(s => s.A).Returns(ASet.Object);
        context.Setup(s => s.B).Returns(BSet.Object);

        var m = new ClassThatImTesting(context);
        m.someMethod("test");
    }

Normally I would see that this is enough to let A and B know about each other in the mock context. 通常,我会看到这足以让A和B在模拟上下文中相互了解。 However, in the method that I'm testing, if I run something like this: 但是,在我正在测试的方法中,如果运行以下命令:

var result = context.A.Where(x => x.theB.Count() > 0).FirstOrDefault();

I get null. 我得到空。 Since I've done this in so many other tests without a problem, I'm having a hard time seeing what I've done to trip this up. 由于我已经在许多其他测试中做到了这一点而没有问题,因此,我很难看到自己做了什么来解决这个问题。 Any ideas would be helpful! 任何想法都会有所帮助! I've simplified this example from my actual code, so please forgive syntax errors. 我已经从实际代码中简化了该示例,因此请原谅语法错误。

You need to instantiate the collection to the A object before injecting the context to the class. 在将上下文注入到类之前,需要实例化集合到A对象。

    [TestMethod]
    public void SomeTest()
    {
        var theAs = new List<A>
        {
            new A{ AId=1, Name="test" }
        };
        var theBs= new List<B>
        {
            new B{ BId=1, AId=1, theA=theAs[0] }
        };
        //here
        theAs[0].theB = theBs

        var ASet= new Mock<DbSet<A>>().SetupData(theAs);
        var BSet= new Mock<DbSet<B>>().SetupData(theBs);

        var context = new Mock<MyContext>();
        context.Setup(s => s.A).Returns(ASet.Object);
        context.Setup(s => s.B).Returns(BSet.Object);

        var m = new ClassThatImTesting(context);
        m.someMethod("test");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM