0

using asp.net core(1.1) and structuremap (v4.4), I want to get instance of my dbContext (to insert seed data, etc.)

how can I get instance of my dbContext? I saw that the normal way is to inject IContainer container into the class constructor, but in my integration test class the constructor will resolved before initialization of the container, (since in my test setup I call the startup class:

_server = new TestServer(new WebHostBuilder()
            .UseEnvironment("testing")
            .UseStartup<Startup>());

and the container initialized in statup class. (var container = new Container();)

now I want to do in my test something like AbContext.Accounts.Add(new Account()); how can I get instance of Abcontext?

I think that with the deprecated static objectFactory - one can do it easily by objectFactory.Container.GetInstanse(). So, I'll be very happy to find the current equivalent for this.

arielorvits
  • 5,235
  • 8
  • 36
  • 61
  • Your setup should work. In my case I do exactly the same, but I use the same Startup from my app. I change only the environment. Post your test and your startup also. – Fabricio Koch Dec 08 '16 at 11:54
  • Is your App working? Because if it's working, your test should also work because it's the same configuration. You just need to start the server as you're doing and create your test correctly. That's why I would like to see your test. – Fabricio Koch Dec 08 '16 at 12:27
  • @FabricioKoch again, all work perfect. I just need an instance of dbContext in my test. how do you get an instance of? for example my test is empty: `[Test]public void test(){AbContext.Accounts.Add(new Account())}` – arielorvits Dec 08 '16 at 12:36
  • Now I understand what you're doing. Your test should not use `AbContext`, you have to access your API. Take a look in this [tutorial](https://github.com/fabriciokoch/AspnetcoreAngular/tree/master/Part2) to see how to access the API in a integration test. – Fabricio Koch Dec 08 '16 at 13:05
  • The Idea of integration test is to test your whole application. If you need some data, you can configure it on your startup. You can have an extra startup just for testing. – Fabricio Koch Dec 08 '16 at 13:15
  • yes, I test my application end to end in my integration test, but I dont see the theoretical problem with seeding the db personally for each integration test, if such option exists, and I think that with the deprecated objectFactory - one can do it easily by objectFactory.Container.GetInstanse(). So, I'll be very happy to find the current solution for this. – arielorvits Dec 08 '16 at 13:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130120/discussion-between-arielorvits-and-fabricio-koch). – arielorvits Dec 08 '16 at 13:35

0 Answers0