mocking multiple interfaces wirth machine.fakes

1.2k Views Asked by At

how can I obtain the equivalent of

Substitute.For<DbSet<MyClass>, IQueryable<MyClass>, IDbAsyncEnumerable>()

with machine.fakes? I tried using

var myFake = An<DbSet<MyClass>>();
myFake.WhenToldTo(m => ((IQueryable<MyClass>)m).Provider).Return(whatever);

but I get the following NotImplementedException:

The member 'IQueryable.Provider' has not been implemented on type 'DbSet`1Proxy' which 
inherits from 'DbSet`1'. Test doubles for 'DbSet`1' must provide implementations of 
methods and properties that are used.

The same exception raises with myFake.WhenToldTo(m => ((IQueryable)m).Provider).Return(whatever);

This is a class that reproduces the issue with minimal code. You need to add packages for Entity Framework, Machine.Specifications, Machie.Specifications.Should, Machine.Fakes, Machine.Fakes.NSubstitute, NSubstitute (or any other Mock Framework plugin)

using System.Data.Entity;
using System.Linq;
using Machine.Fakes;
using Machine.Specifications;

namespace SpecsTests
{
    public class TestClass
    {}

    [Subject("Test")]
    internal class TestSpecifications
    {
        [Subject("Test")]
        private class Test : WithFakes
        {
            private static int Count;
            private static DbSet<TestClass> Subject;

            private Establish context = () =>
                                        {
                                            var data = new [] { new TestClass() }.AsQueryable();
                                            Subject = An<DbSet<TestClass>>();

                                            Subject.WhenToldTo(m => ((IQueryable)m).Provider).Return( data.Provider);
                                        };

            private Because of = () => { Count = Subject.Count(); };

            private It Should_return_expected_results = () =>
                                                        {
                                                            Count.ShouldEqual(1);
                                                        };

        }
    }
}
1

There are 1 best solutions below

3
On BEST ANSWER

There is no way of faking more than one interface in one fake with Machine.Fakes - equivalent to NSubstitute's Substitute.For<>(). I see two options for you:

  1. Use NSubstitute directly.
    You can always do
    Subject = Substitute.For<DbSet<TestClass>, IQueryable<TestClass>>();
    instead of
    Subject = An<DbSet<TestClass>>();
    The drawbacks are that you give up the independence from the underlying mocking framework and Machine.Fake's automocking capabilities

  2. Restructure your code to fake only one interface at a time. This would be my preferred option.
    My first try with this would be to fake IDbSet<> instead of DbSet<> and work only against this interface. I know too little of what you are trying to achieve to give more detailed hints, however. As a general rule, you should always try to depend on interfaces. Maybe you can make the class under test independent of the concrete DbSet<>?