Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Static variables and methods within Owin test server
brightoniant
#1 Posted : Wednesday, July 13, 2016 10:53:26 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 7/13/2016(UTC)
Posts: 8
Location: United Kingdom

Was thanked: 1 time(s) in 1 post(s)
Hi

We are using the Owin Test Server in our codebase to run boundary tests against our API instance. We have the following line of code within a fixture, and the fixture is used across multiple tests. The tests are written using XBehave, which is backed by XUnit 2.

Code:
public TestServer Server { get; } = TestServer.Create<Startup>();


The Startup.cs is from our API project. This subsequently registers the routes, which causes the following exception:

Quote:
System.ArgumentException: A route named 'Default' is already in the route collection. Route names must be unique.


Every single API test fails that uses this fixture, across 9 different test classes.

If I make the Server variable static within the fixture then this problem goes away, however everything I have read makes me feel this shouldn't even be happening?

Can someone give me some guidance about what might be causing this issue?

Cheers
Ant
Remco
#2 Posted : Wednesday, July 13, 2016 12:50:22 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,145

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi Ant,

My first thought is that these routes may be stored statically, so recreating them may be giving the duplication error. The tests would thus be state/sequence dependent. It should be possible to analyse this behaviour by debugging multiple tests within the same execution run, and examining how the later tests in the run behave around this state.

NCrunch has a few differences in how it executes tests inside the test application domain. If you haven't already, it's worth becoming familiar with these differences as they can sometimes trip up tests on subtle ways. See here for more information - http://www.ncrunch.net/documentation/considerations-and-constraints_test-atomicity.
brightoniant
#3 Posted : Wednesday, July 13, 2016 3:28:52 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 7/13/2016(UTC)
Posts: 8
Location: United Kingdom

Was thanked: 1 time(s) in 1 post(s)
Hi Remco

Thanks for getting back to me. Your response pointed me to the right direction. The cause is the reuse of the test runner process across tests.

The Startup class had a static call to RouteTable.Routes. I introduce a new derived class from Startup and made the call to get the routes a virtual method, returning RouteTable.Routes in the Startup class, and a new RouteCollection in my TestStartup class when any routes were found. This is because the presence of any routes meant the static config had already been set up.

The result is code introduced just for NCrunch, but its footprint is lightweight and it's an acceptable pattern to my mind.
Remco
#4 Posted : Thursday, July 14, 2016 1:08:49 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,145

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Great to hear! Nice work on fixing the issue and thanks for confirming!
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.035 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download