Code Monkey home page Code Monkey logo

Comments (7)

jeremydmiller avatar jeremydmiller commented on June 7, 2024

@adamhathcock I do like that idea and it's something I had intended to get around to. We have that with the older FubuMVC version of all of this that we use at work. The Before(), After() stuff that isn't implemented yet was vaguely meant for that.

There's a big caveat though, you'd want to use something like StructureMap's
child container functionality so that your fake services injected for a specific test don't bleed into subsequent tests.

If you only need to inject mocks or stubs upfront, there's some hooks on the SystemUnderTest we could use for that, or at least make sure the ServiceCollection is exposed.

There's a couple caveats though:

from alba.

adamhathcock avatar adamhathcock commented on June 7, 2024

I guess I would want to Mock per test. I want to intercept SQL and Mock the return values to fully integration test.

So essentially, I'd want to resetup the entire SystemUnderTest each test. I'd pass my Mocks to the ForStartup call. By doing this, I wouldn't be worrying about sharing with other tests anyway.

Does this sound like a valid scenario? I feel like I could be misusing something.

from alba.

jeremydmiller avatar jeremydmiller commented on June 7, 2024

"So essentially, I'd want to resetup the entire SystemUnderTest each test" - as a temporary workaround yes, but we'd want something like fubu's model that can isolate service overrides between tests without having to bootstrap all over again. Restarting the app between tests isn't going to scale to any kind of bigger test suite.

"Does this sound like a valid scenario?" -- we do it at work, so it better be valid;-)

I say yes, especially if you have external dependencies you can't control from the test harness. We have a little trouble with that getting overused at work though.

from alba.

adamhathcock avatar adamhathcock commented on June 7, 2024

On the surface, I can't do this easily.

WebHostBuilder.ConfigureServices is always called before the ConfigureServices on the Startup class. Which makes sense. I'd want to configure services after the Startup class is called I guess.

from alba.

adamhathcock avatar adamhathcock commented on June 7, 2024

Then again, I can just inject stuff then make sure I only use TryAdd methods.

from alba.

 avatar commented on June 7, 2024

It is indeed true that WebHostBuilder.ConfigureServices is run before Startup.ConfigureServices, making it impossible to use for overriding service registrations from Startup. However, since .Net Core 2.1 (I think) there is an extension method ConfigureTestServices in Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions that is run after Startup.ConfigureServices.
aspnet/Hosting#1303

So the Alba docs are wrong, and should be corrected:
http://jasperfx.github.io/alba/documentation/bootstrapping/

Change ConfigureServices to ConfigureTestServices here:

var stubbedWebService = new StubbedWebService();

var builder = WebHost.CreateDefaultBuilder()
    .UseStartup<Startup>()
    
    // override the environment if you need to
    .UseEnvironment("Testing")
    
    // override service registrations if you need to
    .ConfigureTestServices(s =>
    {
        s.AddSingleton<IExternalWebService>(stubbedWebService);
    });

// Create the SystemUnderTest, and alternatively override what Alba
// thinks is the main application assembly
// THIS IS IMPORTANT FOR MVC CONTROLLER DISCOVERY
var system = new SystemUnderTest(builder, applicationAssembly:typeof(Startup).Assembly);

Or the simpler form:

var system = SystemUnderTest.ForStartup<Startup>(builder =>
{
	return builder.ConfigureTestServices(s =>
	{
               s.AddSingleton<IExternalWebService>(stubbedWebService);

               // Alternatively, replace the registration
	       //s.Replace(ServiceDescriptor.Singleton(stubbedWebService));
	});
});

Note that if you override with AddSingleton (or similar) there will be two registrations for that service (interface), but the latest one is picked when resolving. It feels safer to me to use Replace. :)


Also, the Alba unit test override_service_registration_in_bootstrapping probably needs to be changed, because I can't see that it actually works as intended since it uses WebHostBuilder.ConfigureServices:
https://github.com/JasperFx/alba/blob/master/src/Alba.Testing/Acceptance/using_custom_service_registrations.cs

@jeremydmiller

from alba.

jeremydmiller avatar jeremydmiller commented on June 7, 2024

Closing this as there actually are some better facilities to deal with this issue now.

from alba.

Related Issues (20)

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.