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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:07:02+00:00 2026-05-30T23:07:02+00:00

Just imagine that my asp.net mvc Controller method could have this code: testplanService.AddTestplan(testplan,template,release); which

  • 0

Just imagine that my asp.net mvc Controller method could have this code:

testplanService.AddTestplan(testplan,template,release);

which calls the below implemented service method:

public TestplanService
{

public void AddTestplan(Testplan testplan, Template template, Release release)
        {
           testplan.CreatedAt = DateTime.Now;
           testplan.UserId = WindowsIdentity.GetCurrent().Name;
           testplan.Name = release.Name + " " + template.Name + " " + testplan.UserId + " " + testplan.CreatedAt;

           _provider.AddTestplan(testplan, template, release);                             
        }

}

How can I test the CreatedAt, UserId and the Name property in the ASSERT when I do not know
the values inside this method?

Yes I know I could pass all 3 values inside the testplan object which is anyway passed to the service method, BUT a Controller class in mvc should not have these 3 lines of logic.

So what would you do?

  • 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-30T23:07:04+00:00Added an answer on May 30, 2026 at 11:07 pm

    Just imagine that my asp.net mvc Controller method could have this
    code:

    testplanService.AddTestplan(testplan,template,release);

    I don’t want to imagine anything like that. The correct way is that your controller works with abstractions, not actual implementations. So instead of depending on an actual service implementation, this testplanService variable that you are using in your controller will be a simple interface or an abstract class that will be injected into the constructor of your controller.

    Now in your unit test you could actually mock this interface and test your controller in isolation.

    Now let’s consider the actual implementation of the service:

    public void AddTestplan(Testplan testplan, Template template, Release release)
    {
        testplan.CreatedAt = DateTime.Now;
        testplan.UserId = WindowsIdentity.GetCurrent().Name;
        testplan.Name = release.Name + " " + template.Name + " " + testplan.UserId + " " + testplan.CreatedAt;
        _provider.AddTestplan(testplan, template, release);                             
    }
    

    There are a couple of issues with this method. First of all it depends on non-deterministic data such as DateTime.Now which are difficult to unit test. Martin Fowler has a nice article about non-determinism in unit tests. It also depends on the current WindowsIdentity. All that’s hard to unit test because your method depends on actual implementations instead of working with abstractions.

    So you will have to abstract those notions behind providers that you can mock in your unit test. Make sure all your components are weakly coupled between them. You will find it much easier to work with them in isolation.


    UPDATE:

    As requested in the comments section here’s an example of how to abstract datetime retrieval:

    Consider the following class:

    public class WeekendTester
    {
        public bool IsWeekendSoon()
        {
            return (int)DateTime.Now.DayOfWeek > 3;
        }
    }
    

    It’s obvious that unit testing IsWeekendSoon is very difficult as it will depend on the day your unit test is run.

    So in order to break the non-determinism in this method you could do the following:

    public class WeekendTester
    {
        private readonly Func<DateTime> _dateTimeProvider;
        public WeekendTester(Func<DateTime> dateTimeProvider)
        {
            _dateTimeProvider = dateTimeProvider;
        }
    
        public bool IsWeekendSoon()
        {
            return (int)_dateTimeProvider().DayOfWeek > 3;
        }
    }
    

    Now all that you need is to configure your favorite DI framework to inject () => DateTime.Now into the constructor of this object and inside your unit test you can easily mock it:

    [TestMethod]
    public void Weekend_Is_Still_Far_Away_From_The_28_th_Of_February_2012()
    {
        // arrange
        Func<DateTime> providerMock = () => new DateTime(2012, 2, 28);
        var sut = new WeekendTester(providerMock);
    
        // act
        var actual = sut.IsWeekendSoon();
    
        // assert
        Assert.IsFalse(actual);
    }
    

    You can see how in this example we are able to force the method work with whatever date we want it to in the unit test in order to verify the correct behavior of the method.

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

Sidebar

Related Questions

Imagine that I have a Spring MVC controller something like this: @Controller @RequestMapping(/base-url) public
just wondering if anyone knows of a truly restful Put/delete implementation asp.net mvc preview
My asp.net page dynamically displays 207 questions (I can't control this). Each question have
I have an ASP.NET MVC application where I need to allow to customers configure
I am working on a asp.net mvc site that uses facebook social widgets. Whenever
I was reading this blog post on ASP.NET MVC 2's new model validation and
just wondering if there is something built into asp.net mvc where I can override
I've been following this guide from MSDN about Creating an ASP.NET MVC Areas Application
I have an .Net MVC 3 web application that has the following structure root
I've never learnt JavaScript, but I imagine this is quite a simple problem. Just

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.