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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:37:38+00:00 2026-05-14T01:37:38+00:00

I have an asp.net mvc2 application that is using StructureMap 2.6 and NHibernate 3.x.

  • 0

I have an asp.net mvc2 application that is using StructureMap 2.6 and NHibernate 3.x. I would like to add unit tests to the application but am sort of at a loss for how to accomplish it.

Say I have a basic controller called Posts that has an action called Index. The controller looks something like:

public class PostsController : Controller {

  private readonly IPostService _postService;
  public PostsController(IPostService postService) {
    _postService = postService;
  }

  public ActionResult Index() {
    return View(_postService.QueryOver<Post>().Future());
  }
}

If I wanted to create an nunit test that would verify that the index action is returning all of the posts, how do I go about that? If mocking is recommended, do you just assume that interaction with the database will work?

Sorry for asking such a broad question, but my web searches haven’t turned up anything decent for how to unit test asp.net mvc actions that use StructureMap (or any other IOC) and NHibernate.

btw, if you don’t like that I return a QueryOver object from my post service, pretend it is an IQueryable object. I’m using it essentially in the same way.

  • 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-14T01:37:38+00:00Added an answer on May 14, 2026 at 1:37 am

    I would refactor your query into the service layer itself. My reason for suggesting this is that you can then have all your security, projections, paging, filtering, etc in one spot. Even if you don’t have these concerns now, it will be much easier to add them in later if everything is not strewn about in different controller actions.

    With this split up you can easily unit test the GetAllPosts() method. (Either mock out your repo or just plug into an in memory database.) As far as testing the controller action, it’s basically a service call at this point so would you gain any benefit from testing it? In my opinion, probably not.

    // service
    public IQueryable<Post> GetAllPosts()
    {
       return postRepository.QueryOver<Post>().Future();
    }

    // controller
    public ActionResult Index() {
    return View(_postService.GetAllPosts());
    }

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

Sidebar

Related Questions

i m using asp.net mvc2 for my application. i have a view that accepts
I have an Asp.net MVC2 web application. I built the application using VS2008 and
I have an ASP.net MVC2 application. In wich I'm using JQuery to alter all
I have a an ASP.NET MVC2 application, with Linq2SQL, that updates a database without
I am using ASP.NET MVC2 application in C#. I have two HTML forms on
I have created a new MVC2 project using the ASP.NET MVC2 Web Application, which
I have ASP.NET web pages for which I want to build automated tests (using
I have a ASP.NET application that we've written our own logging module for. My
I have a asp.net page, and would like to know whether script1 is already
In my ASP.NET MVC2 application, I have a ViewModel class called UserCreateViewModel. In this

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.