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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:43:23+00:00 2026-06-15T12:43:23+00:00

I am new to using Mocks. But what are it’s main purposes? I’m going

  • 0

I am new to using Mocks. But what are it’s main purposes? I’m going to start by using Moq to test my application (and NUnit).

For example, I have code that does things like this:

My webpage code behind:

public partial class MyWebpage
{
    protected string GetTitle(string myVar)
    {
        return dataLayer.GetTitle(myVar);
    }
}

My data access layer:

public class DataLayer
{
    public string GetTitle(string myVar)
    {
        // Create the query we want
        string query = "SELECT title FROM MyTable " + 
            "WHERE var = @myVar";

        //ENTER PARAMETERS IN HERE

        // Now return the result to the view
        return this.dataProvider.ExecuteMySelectQuery(
            dr =>
            {
               //DELEGATE DATA READER PASSED IN AND TITLE GETS RETURNED
            },
            query,
            parameters);
    }
}

My data provider talks and interacts directly with the db:

 public class DataProvider
{
     public T ExecuteMySelectQuery<T>(Func<IDataReader, T> getMyResult, string selectQuery, Dictionary parameters)
    {
          //RUNS AND RETURNS THE QUERY
     }
}

What’s the best way to test all of this?

  • 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-15T12:43:24+00:00Added an answer on June 15, 2026 at 12:43 pm

    If you want to test the layers separately, you would need to create interfaces for your DataProvider and DataLayer classes that expose the methods that you want to Mock. Then you can use a mocking framework – NSubstitute is very good, less code to write to create the mocks – to mock out the calls to the dependent classes, leaving you to test the code within that specific unit

    public interface IDataProvider
    {
         T ExecuteMySelectQuery<T>(Func<IDataReader, T> getMyResult, string selectQuery, Dictionary parameters);
    }
    
    public interface IDataLayer
    {
        string GetTitle(string myVar);
    }
    
    public class DataLayer 
    {
        private IDataProvider dataProvider;
    
        public DataLayer(IDataProvider dataProvider)
        {
            this.dataProvider = dataProvider;
        }
    }
    

    Then, in your test code, you create mocks instead of real objects and pass those into the constructor when you instantiate your test objects. To test the DataLayer:

    [Test]
    public void WhenRetievingTitleFromDataStore_ThenDataLayerReturnsTitle() 
    {
        var title = "Title";
        var dataProviderMock = new Mock<IDataProvider>(MockBehavior.Strict);
        dataProviderMock.Setup(x => x.ExecuteMySelectQuery(<parameters>)).Returns(title);
        var dataLayer = new DataLayer(dataProviderMock.Object);
        Assert.That(dataLayer.GetTitle(It.IsAny<string>(), Is.EqualTo(title));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Sharp Architechture and Rhino Mocks with NUnit. I have a test
I am using .NET 4, NUnit and Rhino mocks. I want to unit test
I am fairly new to TDD but have been using it for long enough
I am using Moq, NUnit, WPF, MVVM, Ninject. I am writing a test for
could anyone give me a good example of using rhino mocks, nunit, and unity
I'm new using Yii framework. I show a list a checkbox. But it's not
I am new to using Jade -- and it's awesome so far. But one
I have a typical Silverlight application with a WCF service and I am using
I'm new to using Moq and I cannot find the way for doing this.
I am using the new Web API bits in a project, and I have

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.