单元测试使用本机代码的c#项目

时间:2012-03-21 05:44:37

标签: c# visual-studio-2010 unit-testing c++-cli unittest2

我有三个项目

1)包含完整业务逻辑的非托管c ++

2)C ++ / CLI(项目名称管理

3)C#GUI

我在C ++ / CLI中添加了非托管c ++的库文件,然后在C#项目中添加了C ++ / CLI的dll。这一切都运行正常,执行很麻烦。

现在我想对C#函数进行单元测试,该函数调用C ++ / CLI包装器,然后返回结果。我使用Visual Studio 2010创建了一个单元测试。我在我的测试项目中添加了C ++ / CLI的dll 。当我尝试执行测试时,它会抛出异常managed.dll未找到

这是代码

   [TestMethod()]
   public void getstateTest()
   {
    bool expected=true;
    bool actual=false;
    try
     {
        GUI.test target = new test();
        expected    = true; // TODO: Initialize to an appropriate value
        actual      = target.getstate();
     }
     catch (FileNotFoundException exception)
     {
        MessageBox.Show("Missing file is : " + exception.FileName);
     }
     Assert.AreEqual(expected, actual);
    }



    The getstate function is

namespace GUI
{
public class test
{
    public bool getstate()
    {
        bool chk = false;
        bool result;
        String a = "some path";
        String b = "some path"
        String c = "some path"
        managed  objct;
        objct = new  managed();
        objct.Initialize(a, b, c, chk);
        objct.Execute();//calls the C++/CLI execute which calls unmanaged C++
        result = objct.Executionresult();//gets a bool result
        return result;
    }
}

}

同样的事情在我运行应用程序时工作正常但在运行测试项目时它表示缺少dll

很抱歉,如果我让它变得混乱。请询问您是否需要更多信息。谢谢提前

2 个答案:

答案 0 :(得分:1)

你看过测试项目输出文件夹了吗?也许managed.dll没有被复制到输出。

更新

请你从异常中发布融合日志吗?这应该给出一些未找到文件的想法。

答案 1 :(得分:0)

项目可能正在编译到不同的目录。

这会导致编译器正确编译,但在运行时它无法找到文件。

当您将单独的项目导入新解决方案时,通常会发生这种情况。

将新的Unit测试编译到同一目录。