Code Monkey home page Code Monkey logo

lightinject.xunit's Introduction

LightInject.xUnit

LightInject.xUnit provides an integration that enables dependency injection in xUnit test methods.

Installing

LightInject.xUnit provides two distribution models via NuGet

Binary

PM> Install-Package LightInject.xUnit

This adds a reference to the LightInject.Xunit.dll in the target project.

Source

PM> Install-Package LightInject.xUnit.Source

This will install a single file, LightInject.Xunit.cs in the target project.

Injecting services

Services from LightInject are injected into methods that are decorated with the InjectData attribute.

[Theory, InjectData]                
public void TestMethod(IFoo foo)
{
    Assert.NotNull(foo);
}

Configuration

LightInject will look for an ICompositionRoot implementation in the same assembly as the requested service. If it is found, it will be executed and the container gets configured through that composition root.

If such an implementation does not exists or that we for some other reason need to configure the container, we can do this by simply implementing a static method in the test class with the following signature.

public static void Configure(IServiceContainer container)
{
    container.Register<IFoo, Foo>();            
}

This method is executed regardless of other composition roots and allows customized configuration of the container before the test is executed.

Scoping (xUnit <= 1.9.2)##

Services registered with the PerScopeLifetime or PerRequestLifetime needs to be resolved within an active Scope to ensure that any services that implements IDisposable are properly disposed.

By decorating the test method with the ScopedTheory attribute, a new Scope will be started when the test method starts and it will end when the test method ends.

[ScopedTheory, InjectData]
public void MethodWithScopedArgument(IFoo foo)
{
    Assert.NotNull(foo);
}

Scoping (xUnit >= 2.0.0)##

Services registered with the PerScopeLifetime or PerRequestLifetime needs to be resolved within an active Scope to ensure that any services that implements IDisposable are properly disposed.

By decorating the test method with the Scoped attribute, a new Scope will be started when the test method starts and it will end when the test method ends.

[Theory, Scoped, InjectData]
public void MethodWithScopedArgument(IFoo foo)
{
    Assert.NotNull(foo);
}

lightinject.xunit's People

Contributors

geirsagberg avatar seesharper avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

lightinject.xunit's Issues

Upgrade to LightInject 5.0.0

how to inject Xunit Logger

Hi, Wanted to inject ITestOutputHelper outputHelper (received in test class constructor) in order to write logs to xuint output, so I need this instance to configure my serilog logger sink.

Don't know how to do it from the static method, since it's executed before my constructor

serviceRegistry.Register<Serilog.ILogger>(factory =>
{
const string logTemplate =
"[{Timestamp:HH:mm:ss.fff%K} | {SourceContext}] | [{Level:u3}]{NewLine}{Message:lj}{NewLine}{Exception}";
var logger = new Serilog.LoggerConfiguration()
.MinimumLevel.Is(LogEventLevel.Error)
.Enrich.FromLogContext()
.WriteTo.Trace(outputTemplate: logTemplate)
.WriteTo.Console(outputTemplate: logTemplate)
.WriteTo.TestOutput(factory.GetInstance(),restrictedToMinimumLevel:LogEventLevel.Verbose, outputTemplate:logTemplate)
.CreateLogger();
return logger;
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.