Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6669361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:08:26+00:00 2026-05-26T03:08:26+00:00

I am using Moq, NUnit, WPF, MVVM, Ninject. I am writing a test for

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:08:27+00:00Added an answer on May 26, 2026 at 3:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing unit tests using NUnit and Moq with my Silverlight code
I'm using Nunit and Moq to test my asp.net mvc solution. Is this a
I'm using NUnit and Moq to test a class which uses a generic WCF
I'm using Moq & NUnit as a unit test framework. I've written a method
I am using nuit with moq to test my controllers. I use a session
I'm working on getting some legacy code under test, and we're using Moq 4
I'm using Moq to create a Mock<HttpResponseBase> to test an FileResult I'm creating for
I am using Moq to mock my Repository layer so I can unit test.
I'm brand new to Moq (using v 4) and am struggling a little with
I am using constructor injection pattern to insert my mocks with moq. So I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.