单元测试项目orleans文件未找到异常

时间:2016-03-07 12:39:11

标签: c# orleans

我正在尝试处理单元测试一个相当简单的应用程序,但我在运行测试时遇到了一个非常奇怪的错误。

我所做的只是:

  • 创建了一个新的类库
  • 添加了对Microsoft.Orleans.TestingHostMicrosoft.Orleans.OrleansProviders版本1.1.2
  • 的引用
  • 运行测试并获得file not found exception

测试文件如下所示:

[TestFixture]
public class HelloWorldTests : TestingSiloHost
{
    //public HelloWorldTests()
    //    : base(new TestingSiloOptions
    //           {
    //               SiloConfigFile = new FileInfo(@"C:\_Foo\Host\Tests\OrleansConfigurationForTesting.xml")
    //           },
    //          new TestingClientOptions
    //          {
    //              ClientConfigFile = new FileInfo(@"C:\_Foo\Host\Tests\ClientConfigurationForTesting.xml")
    //          })
    //{
    //}

    [Test]
    public async Task When()
    {
        var grain = GrainFactory.GetGrain<IHelloGrain>(0);
        var reply = await grain.SayHello("Hello");

        Assert.That(reply, Is.EqualTo("Hello World"));
    }
}

^^从上面我甚至尝试对配置文件的位置进行硬编码。

Stack Trace(有趣的是它在我的 appdata文件夹中寻找配置文件

OneTimeSetUp: System.Exception : Exception during test initialization: 
Exc level 0: System.Runtime.Serialization.SerializationException: Type is not resolved for member 'Orleans.Runtime.Silo+SiloType,OrleansRuntime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at Orleans.TestingHost.TestingSiloHost.LoadSiloInNewAppDomain(String siloName, SiloType type, ClusterConfiguration config, AppDomain& appDomain)
   at Orleans.TestingHost.TestingSiloHost.StartOrleansSilo(SiloType type, TestingSiloOptions options, Int32 instanceCount, AppDomain shared)
   at Orleans.TestingHost.TestingSiloHost.<InitializeAsync>d__16.MoveNext()

Exception doesn't have a stacktrace

OneTimeSetUp: System.Exception : Exception during test initialization: 
Exc level 0: System.IO.FileNotFoundException: Could not find file 'C:\Users\***\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12\OrleansConfigurationForTesting.xml'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
   at System.IO.StreamReader..ctor(String path)
   at System.IO.File.OpenText(String path)
   at Orleans.Runtime.Configuration.ClusterConfiguration.LoadFromFile(String fileName)
   at Orleans.TestingHost.TestingSiloHost.StartOrleansSilo(SiloType type, TestingSiloOptions options, Int32 instanceCount, AppDomain shared)
   at Orleans.TestingHost.TestingSiloHost.<InitializeAsync>d__16.MoveNext()

Exception doesn't have a stacktrace

OneTimeSetUp: System.Exception : Exception during test initialization: 
Exc level 0: System.Runtime.Serialization.SerializationException: Type is not resolved for member 'Orleans.Runtime.Silo+SiloType,OrleansRuntime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at Orleans.TestingHost.TestingSiloHost.LoadSiloInNewAppDomain(String siloName, SiloType type, ClusterConfiguration config, AppDomain& appDomain)
   at Orleans.TestingHost.TestingSiloHost.StartOrleansSilo(SiloType type, TestingSiloOptions options, Int32 instanceCount, AppDomain shared)
   at Orleans.TestingHost.TestingSiloHost.<InitializeAsync>d__16.MoveNext()

Exception doesn't have a stacktrace

2 个答案:

答案 0 :(得分:2)

精神分裂,

如果您使用的是MSTest框架,只需为其抱怨的每个项目添加[DeploymentItem("missing_file_or_dll_name")]即可。如果您使用的是xunit,则应将引用的DLL复制到输出目录,并将所有额外文件(如配置文件)设置为“Copy To Output”。

如果您还有其他需要,请告诉我。

答案 1 :(得分:1)

有点旧,但值得在这里......

这是NUnit和/或其VS适配器的怪癖。

奥尔良会扫描当前目录中的文件,但NUnit会将其设置在其他位置,而这些文件都没有。

因此,您必须在奥尔良启动之前自己设置当前目录。

将这样的内容放在测试的命名空间中:

[SetUpFixture]
public class PreFixture
{
    [OneTimeSetUp]
    public void SetUp() {
        Directory.SetCurrentDirectory(TestContext.CurrentContext.TestDirectory);
    }

}