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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:46:46+00:00 2026-06-11T04:46:46+00:00

The first line of my action is tested with an extra integration test. The

  • 0

The first line of my action is tested with an extra integration test.
The second line is tested with an extra unit test for automapper mapping stuff
The third line is untested then.

The below unit test does it test the third line? because it just tests the return type.

This seems stupid or too trivial too me. What else should the method return????

There is no if/else logic inside this action. Therefore just testing for type == JsonNetResult seems superfluid for me. I would not even realize in my unit test if someone removes the success = true anonymous type.

Should I rather test the data of the JsonNetResult with QUnit?

I would be glad about some guidance and tips because the actions returning Json data drive me crazy… Its just fetch data from db and put it inside the JsonNetResult object.

Action

[HttpGet]
public ActionResult GetTemplateRootUnits(int templateId)
{
    IEnumerable<Unit> units = _dataProvider.GetTemplateRootUnits(templateId);
    IEnumerable<UnitTreeViewModel> unitTreeViewModels = Mapper.Map<IEnumerable<Unit>, IEnumerable<UnitTreeViewModel>>(units);
    return new JsonNetResult(new { data = unitTreeViewModels, success = true });
}

Unit test

[Test]
public void GetTemplateRootUnits_TemplateExists_ReturnsJsonNetResult()
{
    // ARRANGE  
    Mock<IUnitDataProvider> mock1 = new Mock<IUnitDataProvider>();
    Mock<IMappingEngine> mock2 = new Mock<IMappingEngine>();
    Mock<ControllerContext> mock3 = new Mock<ControllerContext>();
    UnitController controller = new UnitController(mock1.Object, mock2.Object);
    mock1.Setup(m => m.GetTemplateRootUnits(1)).Returns(new List<Unit> 
    { 
        new Unit{ UnitId = 1, Name = "Name1", HasChildren = false},
        new Unit{ UnitId = 2, Name = "Name2", HasChildren = false},
        new Unit{ UnitId = 3, Name = "Name3", HasChildren = false},
    });

    var unitTreeViewModels = new List<UnitTreeViewModel> 
    { 
        new UnitTreeViewModel { Id = 1, Name = "Name1", HasChildren = false},
        new UnitTreeViewModel { Id = 2, Name = "Name2", HasChildren = false},
        new UnitTreeViewModel { Id = 3, Name = "Name3", HasChildren = false},
    };

    // Thats the way AutoMapper is mocked
    mock2.Setup(m => m.Map<IEnumerable<Unit>, IEnumerable<UnitTreeViewModel>>(It.IsAny<IEnumerable<Unit>>())).Returns(unitTreeViewModels);

    // ACT
    ActionResult result = controller.GetTemplateRootUnits(1);

    // ASSERT - check that the dataProvider.GetTestplanRootUnits() was called
    mock1.Verify(m => m.GetTemplateRootUnits(1));

    // ASSERT
    Assert.IsInstanceOfType(typeof(JsonNetResult), result);
} 

JsonNetResult.cs

public class JsonNetResult : ContentResult
    {
        private readonly object _data;
        public JsonNetResult(object data)
        {
            _data = data;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            Content = JsonConvert.SerializeObject(_data);
            ContentType = "application/json";
            base.ExecuteResult(context);
        }
        public object Data { get { return _data; } }
    }
  • 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-06-11T04:46:48+00:00Added an answer on June 11, 2026 at 4:46 am

    You haven’t shown what the JsonNetResult class is, but I will assume that it is some custom action result using JSON.NET as serializer instead of the default JavaScriptSerializer. I will also assume that this class exposes a public property of type object called Data holding the model that is to be serialized:

    public class JsonNetResult: ActionResult
    {
        public JsonNetResult(object data)
        {
            Data = data;
        }
        public object Data { get; private set; }
    
        ...
    }
    

    So you could test the data inside the result:

    // ASSERT
    Assert.IsInstanceOfType(typeof(JsonNetResult), result);
    
    var jsonResult = result as JsonNetResult;
    var data = new RouteValueDictionary(jsonResult.Data);
    Assert.IsTrue((bool)data["success"]);
    Assert.IsInstanceOfType(data["data"], typeof(IEnumerable<UnitTreeViewModel>));
    Assert.AreEqual(unitTreeViewModels, data["data"]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#!/bin/bash echo 'first line' >foo.xml echo 'second line' >>foo.xml I am a total newbie
I want to cancel the second line item instead of the first. Below is
The first line of bin/jekyll of the Jekyll gem uses the following syntax: $:.unshift
I'm printing the first line from a file: with open(path,r,encoding='utf8') as f: for i,
My GZipStream will only decompress the first line of the file. Extracting the contents
How do you echo only the first line of print print_r? More info: I
I have text file with something like first line line nr 2 line three
Is it possible to get the first line of text from a UITextView. I
session_start() should be the first line of web page. Dreamweaver's php templates should be
I'm trying to select the first line item ( <li> ) with the class

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.