Testing Castle windsor Component with PerWebRequest lifestyle

后端 未结 4 2037
夕颜
夕颜 2020-12-31 02:00

I\'m trying to do some testing with castle windsor involved, in one of my tests I want to check the windsor installers, so I check that the container can resolve my componen

4条回答
  •  被撕碎了的回忆
    2020-12-31 02:46

    In your test you could subscribe to the ComponentModelCreated event and change the lifestyle of your per-web-request components to something else. (example).

    If you're writing an integration test with the scope of a single request, singleton should do.

    If you're writing an integration test that spans multiple requests, you could use a contextual lifestyle to simulate the scope of requests.

    Edit: including code from example (which is no longer available):

    container.Kernel.ComponentModelCreated += Kernel_ComponentModelCreated;
    

    void Kernel_ComponentModelCreated(Castle.Core.ComponentModel model)
    {
        if (model.LifestyleType == LifestyleType.Undefined)
            model.LifestyleType = LifestyleType.Transient;
    }
    

提交回复
热议问题