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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:46:59+00:00 2026-05-26T21:46:59+00:00

I have a model which contains a link to sql query that fetches information

  • 0

I have a model which contains a link to sql query that fetches information from my db then I loop it out in my view which renders a partial with the resultset in a table. Quite simple and straightforward. Although as I’m really struggling to get better at doing TDD I want to have a test for this. Bare in mind that if you find a test that forces me to rethink my design that’s just fine. Tests should help you see flaws in your designpatterns. I will post some of the code below of my model, and partial:

public class ApplicationModel
{
    public static DeployEntities DeployEntities = new DeployEntities();

    public static IQueryable<Application> GetApplicationList()
    {
        var applications = DeployEntities.Applications.OrderByDescending(a => a.Name);
        return applications.AsQueryable<Application>().Distinct();
    }
}


@{  
  foreach (var a in Deploy.Models.ApplicationModel.GetApplicationList())
                    {      
                <tr id="applications">
                    <td class="row-heading">
                        <p class="pseudo">@a.Name</p>  
                    </td> 
                    <td class="insert-col">
                    <div class="number-insert">
                        <p>Deploy Number</p>
                        <span class="count"></span>
                    </div>

So any thoughts on how I could go about with a test for this. I’m using MVC3 as you can see from the Razor syntax, and Entity Framework. Thanks in advance.

  • 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-26T21:47:00+00:00Added an answer on May 26, 2026 at 9:47 pm

    Although as I’m really struggling to get better at doing TDD I want to have a test for this.

    The following line ruins your model:

    public static DeployEntities DeployEntities = new DeployEntities();
    

    A static hardcoded dependency => impossible to unit test your model in isolation.

    In order to weaken the coupling between your DAL and model you could introduce an abstraction:

    public interface IApplicationRepository
    {
        IQueryable<Application> GetApplications();
    }
    

    and then your model could use constructor injection:

    public class ApplicationModel
    {
        private readonly IApplicationRepository _repository;
        public ApplicationModel(IApplicationRepository repository)
        {
            _repository = repository;
        }
    
        public static IQueryable<Application> GetApplicationList()
        {
            var applications = _repository.OrderByDescending(a => a.Name);
            return applications.AsQueryable<Application>().Distinct();
        }
    }
    

    Now in a unit test you could use a mocking framework such as Rhino Mocks or Moq to mock this repository and set expectations on it and be able to unit test the GetApplicationList method in isolation.

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

Sidebar

Related Questions

I have a jQuery Model windows which contains a form. On creating of the
i have an entity-model file (edmx) file which contains few tables and few stored
I have a need for a model(?) on my app which basically contains a
I have a model which gets its data from a parser object. I'm thinking
I have a Note model, which can contain have either an image link attachment
I have a Note model, which can contain have either an image link attachment
I have a view with a grid that contains items added to a workstation.
I have a model which contains FileField as below class Employer(models.Model): logo = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT),
I have a jquery-ui-dialog that contains a <form> which contains a <input type=file >
I have an object which contains models for my ASP.NET MVC web app. The

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.