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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:46:47+00:00 2026-05-19T02:46:47+00:00

As the title suggests I’m trying to test a method, unfortunately I appear to

  • 0

As the title suggests I’m trying to test a method, unfortunately I appear to be going wrong some where. The method should only return customers that have and ID = 1

Here is my test

        [TestMethod]
        public void Returns_Correct_Number_Of_Workout_Dates_For_Valid_UserId()
        {

        //Arrange
        List<GymSession> gymSessions = new List<GymSession>();

        Customer cust = new Customer();

        cust.CustomerId = 1;

        gymSessions.Add(new GymSession() { Customer = cust, SessionId = 1, Date = new DateTime(2010, 1, 1)  });
        gymSessions.Add(new GymSession() { Customer = cust, SessionId = 2, Date = new DateTime(2010, 1, 2) });
        gymSessions.Add(new GymSession() { SessionId = 3, Date = new DateTime(2010, 1, 3) });
        gymSessions.Add(new GymSession() { Customer = cust, SessionId = 4, Date = new DateTime(2010, 1, 4) });

        var mockRepos = new Moq.Mock<IGymSessionRepository>();
        mockRepos.Setup(g => g.GymSession()).Returns(gymSessions.AsQueryable());

        //Act
        var result = mockRepos.Object.GetWorkoutDatesByCustomerId(1);

        //Assert
         Assert.AreEqual(3, result.Count());
        }

Here is the repository method I’m trying to test

        public IQueryable<GymSession> GetWorkoutDatesByCustomerId(int userId)
    {
        var gymSess = db.GymSessions.Where<GymSession>(g => g.Customer.CustomerId == userId);

        return gymSess;
    }

The idea is that setup has all the customers, and the method then filters them. The count never seems to apply the filter. Any ideas?

  • 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-19T02:46:48+00:00Added an answer on May 19, 2026 at 2:46 am

    It appears that you really want to stub the call to db.GymSessions and that you should test a concrete GymSessionRepository instance. Traditionally, there are two ways to do this (apart from intercepting calls using aspect-oriented programming):

    1) Give your repository an explicit dependency on db and require it in the repository constructor. Here’s what I mean, where I’m using IDatabase to represent db:

    public class GymSessionRepository: IGymSessionRepository {
        private IDatabase db;
        public GymSessionRepository(IDatabase db) {
            this.db = db;
        }
    }
    
    // Then in your test ...
    var mockDb = new Moq.Mock<IDatabase>();
    mockDb.Setup(d => d.GymSessions()).Returns(gymSessions.AsQueryable());
    
    GymSessionRepository repository = new GymSessionRepository(mockDb);
    // ... and so on
    

    2) (Less desirable, but sometimes necessary) Expose the method you want to stub as a virtual member, mock the concrete object you’re testing, and stub the behavior directly on the class under test:

    public class GymSessionRepository {
        // Because this is virtual, you can override it in your mock object
        protected virtual List<GymSession> GymSessions() {
            return this.db.GymSessions.AsQueryable();
        }
    }
    
    // In your test code here: notice the mock object is your concrete class,
    // because your test targets another method on that class, not a method 
    // on an arbitrary implementation (like a mock based on its interface)
    var mockRepos = new Moq.Mock<GymSessionRepository>();
    
    // Override its virtual method; take control for your test
    mockRepos.Setup(g => g.GymSessions()).Returns(gymSessions.AsQueryable());
    

    Depending on the mocking framework, the second technique is known as using a transparent or partial mock. If you find yourself using it often, it may be a sign that your code is overly-coupled (and it can get confusing fast).

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

Sidebar

Related Questions

As the title suggests, I'm trying to write some C# code to interop with
As the title suggests I am trying to add an event listener to a
As the title suggests I'm simply trying to get a named window to come
As the Title suggests, i am trying to find how to insert image in
Just like the title suggests, I'm trying to parameterize the XPath for a modify()
As the title suggests I've been trying to trigger a onmousedown even and then
As the title suggests, I am trying to nest - or create an array
As the title suggests, I'm trying and failing to get the following combination working
As the title suggests, what is the best method for converting an array of
As the title suggests i'm trying to detect the capitalization of a string and

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.