简体   繁体   中英

Run other project when running unit tests in Visual Studio 2013

I have a setup with some VS projects which depend on each other. Simplified, imagine these projects:

  1. API Project (ASP.NET WebAPI 2)
  2. DummyBackendServer (WinForms with a WCF Service)
  3. API.Test Project (Unit Test Project which should Test API-Project's Controllers)

Usually, there would be (for example) an android client, which connects to the API-Server (1) and requests some information. The API-Server would then connect to the DummyBackendServer (2) to request these information, generate an appropriate format and send an answer to the android client.

Now I need to create some unit tests for the API-Server. My problem is, that I don't find a way to tell VS to start the DummyBackendServer (2), when it runs the unit tests. When I first start the DummyServer I can't run the tests, because the menu option is grayed out.

So, is there any way to tell VS to start another project the tests depend on?

For anyone who doesn't want to unit test the correct way and just want to run multiple projects in the same soluition, here is the answer.

Rightclick the backend project -> Debug -> Start without debugging .
The interface will not grey out so you can start other projects.

Start test with rightclick -> Run Tests
Or run your frontend with debugging as usual by having it set as startup project (Bold in the Solution Explorer) and clicking F5 or the green arrow, Start With Debugging.

Divide and conquer!

If the test (some will say that those are not unit test, but that is not part of this question) requires some services to be up - make that happen, Have them deployed to some dev or staging environment. then you only need to configure the connection from the API test assembly.

I would split the solution in two and call them integration tests. If you want them to bee unit test you have what you need from the post above.

You should use the IoC containers or something similar in your project, so you can get the mock of your other projects while run the Unit Tests.

Which one you'll select is up to you, personally I use Rhino.Mocks :

  1. Create a mock repository:
MockRepository mocks = new MockRepository();
  1. Add a mock object to the repository:
 ISomeInterface robot = (ISomeInterface)mocks.CreateMock(typeof(ISomeInterface));  
 //If you're using C# 2.0, you may use the generic version and avoid upcasting:

 ISomeInterface robot = mocks.CreateMock<ISomeInterface>();
  1. "Record" the methods that you expect to be called on the mock object:
// this method has a return type, so wrap it with Expect.Call
Expect.Call(robot.SendCommand("Wake Up")).Return("Groan");

// this method has void return type, so simply call it
robot.Poke();

//Note that the parameter values provided in these calls represent those values we  
//expect our mock to be called with. Similary, the return value represents the value  
//that the mock will return when this method is called.

//You may expect a method to be called multiple times:

// again, methods that return values use Expect.Call
Expect.Call(robot.SendCommand("Wake Up")).Return("Groan").Repeat.Twice();

// when no return type, any extra information about the method call
// is provided immediately after via static methods on LastCall
robot.Poke();
LastCall.On(robot).Repeat.Twice();
  1. Set the mock object to a "Replay" state where, as called, it will replay the operations just recorded.
mocks.ReplayAll();
  1. Invoke code that uses the mock object.
theButler.GetRobotReady();
  1. Check that all calls were made to the mock object.
mocks.VerifyAll();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM