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

  • Home
  • SEARCH
  • 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 8323343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:41:56+00:00 2026-06-08T23:41:56+00:00

I am working on this WebAPI project and I need to create unit tests

  • 0

I am working on this WebAPI project and I need to create unit tests for it. The base of the project was created using VS 2010 , and then an WebApi Module was added.

The workings of the controller are kinda getting in the way of testing/mocking. I am using xUnit and Moq , but there is absolutely no need to stick with those two.

The application uses specific objects to deal with database access, so I have the
controllerObject and its contructor requires the DataAccessObject

I am having problem with mocking the controller / dataaccess pair. The first test i´m trying to run is a GetAllFromDataBase, but i dont really have a clue on how to do it.

EDIT:

I did what Cuong Le told me and the whole is moving now, i really apreciate it. But i stumped into another problem. To access the API, there is a username/password pair and my controller uses Thread.CurrentPrincipal.Identity.Name; right now i would need to set this value for it to fully work I guess.

Also the valueServiceMock.Setup(service => service.GetValues())
.Returns(new[] { “value1”, “value2” });

does not seem to be working. as the code tries to reach for the database, and gets nothing since it cant get a valid username to look for

  • 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-08T23:41:57+00:00Added an answer on June 8, 2026 at 11:41 pm

    In order to get your app testable, you need to design for testability in mind.Technically, to design to testability, your app should be loose coupling as much as possible between layers, between components and even between classes.

    A lot of hints to design for testability: avoid sealed, static class… But the most popular thing you need to be aware of is dependency injection pattern, instead of creating object inside contructors or methods of other objects, this object should be injected. With this way we make loose dependency between class and easy for us to fake by mocking framework. Esp, for the objects which depend on external resource: network, file or database.

    How to inject object by using dependency injection: that’s why IocContainer is the right tool for this, it will inject objects for you automatically. IoC Container which I prefer to use is: Autofac and NInject.

    Example in here to inject ValueService into ValuesController:

    public class ValuesController : ApiController
    {
        private readonly IValueService _valueService;
    
        public ValuesController(IValueService valueService)
        {
            _valueService = valueService;
        }
    
        public string[] Get()
        {
            return _valueService.GetValues();
        }
    
        public string Get(int id)
        {
            return _valueService.GetValue(id);
        }
    }
    

    And below is the simple code to unit test with Moq:

    var valueServiceMock = new Mock<IValueService>();
    valueServiceMock.Setup(service => service.GetValues())
                .Returns(new[] { "value1", "value2" });
    
    var controller = new ValuesController(valueServiceMock.Object);
    var values = controller.Get();
    
    Assert.AreEqual(values.Length, 2);
    Assert.AreEqual(values[0], "value1");
    Assert.AreEqual(values[1], "value2");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this was working on this project a few months ago. I'm using Google
I'm working on a little Android project in java, I'm using the webapi of
I'm currently working on a small webapp where I work. I've created this for
I'm a LAMP guy and ended up working this small news module for an
This is supposed to be a working project from my friend. He demonstrated the
I'm working on a JSF 2.0 project using Mojarra, PrimeFaces and Tomcat 6.x, but
I'm working on a project that uses wcf webapi to expose all it's functionality
I'm currently working on a project where I'm unit testing with NUnit, mocking with
I'm working on a ASP.NET WebApi (Release Candidate) project where I'm consuming several DTOs
I am working on a web development project using flask and sqlalchemy orm. My

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.