为什么单元测试项目需要为其正在测试的类安装 NuGet 包

Why does a Unit Test project need to install NuGet packages for classes it is testing

提问人:Sigmundur 提问时间:11/9/2023 最后编辑:Sigmundur 更新时间:11/9/2023 访问量:14

问:

我有一个项目(ProjectA)使用Microsoft.Graph以及与该包相关的其他内容。我还有另一个项目(ProjectB)在ProjectA中对方法进行单元测试。当我在 ProjectB 中从 ProjectA 创建对象时,它抱怨它无法加载 ProjectA 引用的文件或程序集。

ProjectA 构造函数:

public GraphApi(string tenantId, string clientId, string clientSecret)
{
    string[] scopes = new[] { "https://graph.microsoft.com/.default" };
    // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
    var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
    graphClient = new GraphServiceClient(clientSecretCredential, scopes);
}

ProjectB测试方法:

[TestMethod]
public void GetGroups()
{
    GraphApi graphApi = new GraphApi(TenantId, ClientId, Secret);
}

当单元测试运行时,PojectA 构造函数在“var clientSecretCredential =...”行中弹出错误:

System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.Identity.Client, Version=4.57.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae' or one of its dependencies. The system cannot find the file specified.'

Microsoft.Identity.Client 在 ProjectA 中引用,其版本与错误消息 4.57.0.0 中提到的版本相同。

Reference to Microsoft.Identity.Client version 4.57.0.0

我想知道这是否是 ProjectA 中的某种参考地狱导致这种情况,而不是我需要在 ProjectB 中安装它抱怨的包。

为什么会发生这种情况,我该如何进行故障排除?

P.S. 我最近将我的所有项目从 c 驱动器移动到带有驱动器号 s 的新额外驱动器。我在想也许这与此有关,因为我似乎记得这是在我移动项目的时候开始的。

单元测试 DLL 参考

评论


答: 暂无答案