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 7441891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:05:21+00:00 2026-05-29T11:05:21+00:00

This test is to check that I can return a ViewModel object by creating

  • 0

This test is to check that I can return a ViewModel object by creating a customer and calling the Details() controller method.

[TestMethod()]
public void Can_View_AccountDetails()
{
       AccountController target = new AccountController(null, null, null, null);
       target.customer = new Customer { Id = 4, Active = true, BillingAddress_Id=1, ShippingAddress_Id=2 };

       //  Act
       ActionResult result = target.Details();
       //  Assert
       var resultData = ((ViewResult)result).ViewData.Model as AccountViewModel;
       Assert.IsInstanceOfType(resultData, (typeof(AccountViewModel)));
}

‘customer’ is a member of the controller base class, which is then assigned in Initialize(). Initially I couldn’t assign anything to it, but by setting it to ‘public’ rather than ‘protected’ I was able to use it in my test and avoided trying to call the base class Initialize() method.

EDIT: ‘customer’ is populate from a Repository object injected into the base class constructor.

Is this the right way to do this? It seems somehow wrong to change the accessibility level in order to get the test to work.

Also, although I’m trying to use Moq to create my tests, I’m not actually doing any mocking at all here which again, doesn’t seem right.

  • 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-29T11:05:22+00:00Added an answer on May 29, 2026 at 11:05 am

    I think your real problem is that the customer information “magically” shows up within the AccountController. The Customer instance should be injected into the AccountController from the outside since it is an external dependency. That being the case you would not have to make the customer property public because you pass it into the AccountController yourself.

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

Sidebar

Related Questions

Can someone tell me why this unit test that checks for exceptions fails? Obviously
I have action methods that do things like this: productTable.AddOrUpdate(viewModel.Account); I don't currently test
Check out this test: [TestFixture] public class Quick_test { [Test] public void Test() {
check this out: template <class T> class Test: public T { public: void TestFunc()
My point is this. For test i need when user check chk1 the chk2
How can this Possibly return non-nil on iPhone 3.1.3 (3g iPhone)? - (BOOL) isIOS3_2OrAbove
This is just a sanity check, but I've done several test cases before you
I need to create a unit test for a method that returns an xmldocument.
I want to make a query using which I can check that URL which
I can't figure out what's going wrong here. This test fails: @Test public void

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.