1

This question is part of larger question that can be found here

As in out production code we use Ninject and constructor injection our services tend to look like this

public class Service : IService
{
    private readonly IRepository _repository;

    public Service(IRepository repository)
    {
        _repository = repository;
    }

    public Task<IEnumerable<SelectOption>> GetAlLogicOptions()
    {
        return  _repository.GetOptionsAsync();
    }
}

how ever list of constructor parameters may and will change over time. This is the reason that we want to have IoC to also be used in tests.

In C# NUnit this is quite easy as we have Ninject.MockingKernel that always provides mock implementation and in each test fixture we just rebind sut to it real implementation.

How to achieve same thing in F# xUnit.

Community
  • 1
  • 1
Icen
  • 441
  • 6
  • 14
  • 1
    F# runs on .NET, just as C# does, so your C# stack ought to still work. Where are you having problems? – Mark Seemann Mar 03 '16 at 07:49
  • As far as i know Ninject.MockingKernel should not be specific to NUnit either, so you should be able to use it with F# xUnit, too. Are you asking on how to use `foq` in MockingKernel? – BatteryBackupUnit Mar 03 '16 at 08:31
  • @MarkSeemann The problem I have is that in Nunit c# there is Test fixture setup and classes can derive from some kind of bootstraper. however in all your examples on pluralsight and YouTube you just write standalone functions. So I was not able to find any F# example where I can configure something like that. – Icen Mar 03 '16 at 15:15
  • @BatteryBackupUnit more or less yes I'm asking for some example where I could see how to set this thing up. And by less I mean I don't need to use F# Foq as any mocking tool can work for me. – Icen Mar 03 '16 at 15:17
  • 1
    I also write standalone 'functions' in C#, and has done so for 5-10 years. That was one of the reasons I created [AutoFixture](https://github.com/AutoFixture/AutoFixture), because I got tired of writing [Fixture Objects](http://blog.ploeh.dk/2009/03/16/FixtureObject) for each Test Fixture... It's an object-oriented tool for an object-oriented problem. I'm not aware of anything similar for F#. Frankly, when writing F# code, I don't miss it. – Mark Seemann Mar 03 '16 at 22:05

0 Answers0