简体   繁体   中英

Using XUnit with Service Fabric

I created a new Service Fabric application using the Stateless Service template. I added a simple class so I can write a unit test for it:

public class FakeClass
{
    public void DoStuff()
    {
        FabricClient client = new FabricClient();
    }
} 

I create a new class library for unit tests and reference the xUnit and xUnit runner nuget packages. When I try and run the unit tests I get the following error:

Result Message: System.BadImageFormatException : Could not load file or assembly 'XUnitExample.WebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

How can I get xUnit working with Service Fabric so I can write unit tests?

The issue apparently has to do with the default setting when creating new projects. When you create a new project it uses Any CPU as the default architecture and Service Fabric requires x64.

I had to go into the Project Build properties and change it use x64 as the target platform. After doing that my unit tests didn't show up in the Test Explorer so I had to change the following setting:

Test -> Test Settings -> Default Processor Architecture -> x64

This issue has nothing to do with service fabric. Can you try reinstalling the xunit package?

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