NUnit / Moq 在变量的构造函数上抛出 NullReferenceException 错误

发布于 2024-12-08 10:38:23 字数 2251 浏览 0 评论 0原文

我正在使用 Moq、NUnit、WPF、MVVM、Ninject。

我正在为 LoginViewModel 编写一个测试,在测试中,当我使用 LoginViewModel 的构造函数创建新实例时,出现 NullReferenceException 错误。代码编译并运行(即,当我运行 LoginView 显示的程序时,并与 LoginViewModel 一起创建正确的行为等),但由于某种原因,UnitTest 崩溃了。

这是构造函数:

public LoginViewModel(ILoginServices loginServices,IDialogService dialogServices)
{
            InitializeFields();
            _loginServices = loginServices;
            _dialogService = dialogServices;
            DomainList = _loginServices.GetDomainListing();

}

我模拟了依赖关系,如下所示:

Mock<ILoginServices> moq = new Mock<ILoginServices>();
 moq.Setup(log =>
           log.LoginUser(It.IsAny<string>(), 
                         It.IsAny<string>(), 
                         It.IsAny<string>()))
           .Callback<string, string, string>((i, j, k) => CheckArgs(i, j, k));

 moq.Setup(log2 =>
           log2.GetDomainListing()).Returns(new List<string> { "Domain" });

 Mock<IDialogService> moq2 = new Mock<IDialogService>();
  • 我还尝试插入真正的服务作为参数。
  • 我已经验证了这些模拟确实有效,并且这些模拟的对象 返回值不为空。
  • 我已经注释掉了构造函数中的所有代码。
  • 我尝试过插入该行

    LoginViewModel 测试 = new LoginViewModel(_fakeLoginService,_fakeDialogService);

    在调用构造函数之前(看看它是否与正在处理的原始局部变量或之前的某些内容有关),并且此行崩溃了。

从我看来,这一定是构造函数(但不是我在其中编写的代码),并且这仅与 NUnit / Moq 有关,因为我的代码仍然可以正常编译和运行。

我对这个人一无所知,有人能指出我正确的方向吗?


[编辑]

好的,我已经运行了代码,错误来自这行代码:

ImageSource = (ImageSource)Application.Current.FindResource(_imageName);

此代码将转到 ImageDictionary 并获取对 WindowViewModel 中撤消按钮的图像的引用(我的 LoginViewModel 继承了它)。

我关于为什么它在应用程序的正常运行中工作但在测试中不起作用的假设是:

1) 因为我通过 NUnit 运行程序代码,所以 Application.Current 对象没有分配属性/没有要获取的 Application.Current 对象。

                                  **or**

2) 与以下事实有关:由于程序代码在 NUnit 中运行,因此代码无法访问/无法解析 ImageDictionary 来查找图像。

我更倾向于第一个假设,但我还不是 100% 确定,而且我在运行时找不到 Application.Current 的值时遇到困难,因为当我将光标移到代码上时,工具提示会出现通常出现的显示未出现的对象的细节。

我的新问题是:这些有道理吗?你们知道在通过 NUnit 运行测试项目时 Application.Current 对象是否存在/可以访问吗?

任何帮助将不胜感激。

I am using Moq, NUnit, WPF, MVVM, Ninject.

I am writing a test for my LoginViewModel, and in the test when I use the constructor of the LoginViewModel to create a new instance, I am getting a NullReferenceException error. The code compiles and runs, (i.e. when I run the program the LoginView shows, and works with the LoginViewModel to create the correct behaviour etc) but for some reason the UnitTest is crashing.

this is the constructor:

public LoginViewModel(ILoginServices loginServices,IDialogService dialogServices)
{
            InitializeFields();
            _loginServices = loginServices;
            _dialogService = dialogServices;
            DomainList = _loginServices.GetDomainListing();

}

I have mocked the dependencies as follows:

Mock<ILoginServices> moq = new Mock<ILoginServices>();
 moq.Setup(log =>
           log.LoginUser(It.IsAny<string>(), 
                         It.IsAny<string>(), 
                         It.IsAny<string>()))
           .Callback<string, string, string>((i, j, k) => CheckArgs(i, j, k));

 moq.Setup(log2 =>
           log2.GetDomainListing()).Returns(new List<string> { "Domain" });

 Mock<IDialogService> moq2 = new Mock<IDialogService>();
  • I have also tried inserting real services as the parameters.
  • I have verified that the mocks do work, and the objects these mocks
    return are not null.
  • I have commented out all the code in the constructor.
  • I have tried inserting the line

    LoginViewModel test = new LoginViewModel(_fakeLoginService,_fakeDialogService);

    in front of the call to the constructor (to see if it had to do with the original local variable being disposed or something before) and this line crashed instead.

From all I can see this must be the constructor,(but not the code I have written inside it) and that this is solely related to NUnit / Moq as my code still compiles and runs fine.

I have no idea on this one guys, can anyone point me in the right direction?


[Edit]

Ok so I have run through the code and the error comes from this line of code:

ImageSource = (ImageSource)Application.Current.FindResource(_imageName);

This code is going to a ImageDictionary and getting a reference to the image for an undo button in the WindowViewModel (which my LoginViewModel inherits).

My hypotheses as to why its working in the normal running of the application, but not in the testing are:

1) Because I am running the program code through NUnit, the Application.Current object isnt getting property assigned/there is no Application.Current object to get.

                                  **or**

2) Something to do with the fact that because the program code is being run in NUnit, the code doesn't have access to/can't resolve the ImageDictionary to find the image.

I'm leaning more strongly to the first hypothesis, but I'm as of yet not 100% sure, and I am having trouble finding the values of the Application.Current at runtime, cause when I move my cursor over the code the tooltip that normally appears showing the detail of the object that is not appearing.

My new question is: Does any of this make sense? Do you guys know if the Application.Current object exists / can be accessed when running the testing project through NUnit?

Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-12-15 10:38:23

你是对的。对于单元测试,Application.Current 为 null。您可以通过注入 Application 对象来解决此问题因为在代码中引用单例会使事情变得棘手。

You are correct. Application.Current is null for Unit tests. You can work around this by injecting the Application object as referencing singletons in code can make life tricky.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文