سؤال

I am trying to get unit tests to run successfully for my MVC 4 Web Application project.

When I run the tests classes individually all of the tests pass, when I come to run all test in solution only 2/9 pass, I have clicked Debug Checked Tests and they all pass when I the hit run again they also all pass.

This problem is also being replicated when I check the project into TFS, I have setup continuous integration they project builds, runs the tests and fails on exactly the same tests.

The error I'm getting back is *"A route named '' is already in the route collection"

Does anybody have any ideas why this might be happening?

In each class I have a [TestInitialize] block which is shown below:

[TestInitialize]
public void Setup()
{
    var builder = new TestControllerBuilder();
    controller = new MyController();
    builder.InitializeController(controller);

    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

I had a similar error in the classes when I hadn't included the TestControllerBuilder, could it be that this code is not running correctly?

هل كانت مفيدة؟

المحلول

RouteTable.Routes is static and therefore will only be initialized once per AppDomain.

So every time you run a test, you are in effect trying to re-register the same routes over again.

You would probably be better off moving your route registration into an AssemblyInitialize attribute so it will only run once at the beginning of the entire test run.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top