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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:01:36+00:00 2026-05-24T01:01:36+00:00

I am testing the functionality of a StoreManager class which has a dependency on

  • 0

I am testing the functionality of a StoreManager class which has a dependency on DataBaseConfiguration class.

public class StoreManager {
  private DataBaseConfiguration dbConfig;
  public void Store(string name) {
    dbConfig.Store(name);
  }
  //other methods here
}

The StoreManager class stores to a database and the only way I can test if this method works fine is to query from the database. I have another class in production which does that..

public class QueryManager {
private DataBaseConfiguration dbConfig;
public string Query(QueryExpression expr) {
    //query logic
    string name = "somename";
    return name;
}}

Eventhough I am concerned with testing only my StoreManager class it looks to me like I need to use the QueryManager class to test the storedvalues.
So I have a basic test case like this one…

[TestFixture]
public class StoreManagerTest {
[TestFixtureSetup]
public void Setup() {
    DatabaseConfiguration dbConfig = new DatabaseConfiguration(/*test database details*/);
    StoreManager sm = new StoreManager(dbConfig);
    QueryManager qm = new QueryManager(dbConfig);
}

[Test]
public void TestStore_ValidStore() {
    sm.Store("testname");
    string queryResult = qm.Query(new QueryExpression("query_expr"));
    Assert.AreSame(queryResult, "testname");
}}

As you can see, apart from the ClassUnderTest (which is StoreManager), the QueryManager class also has a dependency on DatabaseConfig.

I don’t have a lot of logic inside the StoreManager class, it just delegates to the DataBaseConfig class to store (well actually there are some more classes involved in storing, its not the DataBaseConfig that actually stores the data.. but just for simplicity purpose, lets say so..)

I would like to know if there is a better way to handle this test without involving QueryManager at all?
Also is there a better way to inject the dependency on DataBaseConfiguration into the StoreManager class (considering that the DataBaseConfiguration class takes details about the connection string, etc., of the database to store the data into.. and I would like to pass in a test database rather than the production database connection string there).

  • 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-24T01:01:36+00:00Added an answer on May 24, 2026 at 1:01 am

    To get the dependencies out of the way in testing the most common approach is to either use a handwritten stub or a mocking framework (i.e Moq or RhinoMocks).

    Additionally you would have to enable users of the StoreManager class to pass in the DataBaseConfiguration dependency, otherwise you cannot stub it out w/o changing the code. Constructor injection as you do right now is common practice and a clean way to do this (this becomes more convenient if you use an IOC container when you have lots of dependencies), also see here.

    If I understand you correctly, you just want to test that the StoreManager actually stores the value you pass to it, you are interested in the behavior of the StoreManager and its interactions with its dependency DataBaseConfiguration – right now you do that by querying the data store itself for verification.

    Given that let’s go through a bare-bones example using RhinoMocks – the only thing I changed was define the Store method in your DataBaseConfiguration class as virtual so RhinoMocks can override it.

    //Arrange
    string storeValue = "testname";
    var dbConfigMock = MockRepository.GenerateMock<DataBaseConfiguration>();
    dbConfigMock.Expect(x => x.Store(storeValue));
    StoreManager sm = new StoreManager(dbConfigMock);
    
    //Act
    sm.Store(storeValue);
    
    //Assert
    dbConfigMock.AssertWasCalled(x => x.Store(storeValue));
    

    This test verifies that the Store method was called on your DataBaseConfiguration class w/o any other dependencies – it tests the behavior of your StoreManager class. This test does not touch the DB nor does it affect any other classes.

    Edit:

    I’m not sure I understand the concern about using a mocking framework in production code – the mocking framework is only used in your test projects, no reference to it or any code changes are required to the production code itself.

    Using a handwritten stub you can do the same assertions “manually”: Define a test stub that stores how many times and with what value Store() was called (again this requires the Store method to be declared virtual so it can be overridden):

    public class DataBaseConfigurationTest : DataBaseConfiguration
    {
        public int TimesCalled { get; set; }
        public string LastNameStored { get; set; }
    
        public DataBaseConfigurationTest()
        {
            TimesCalled = 0;
        }
    
        public override void Store(string name)
        {
            TimesCalled++;
            LastNameStored = name;
        }
    }
    

    Now use this test stub in your test instead of the “real” DataBaseConfiguration:

    string storeValue = "testname";
    DataBaseConfigurationTest dbConfigStub = new DataBaseConfigurationTest();
    StoreManager sm = new StoreManager(dbConfigStub);
    
    sm.Store(storeValue);
    
    Assert.AreEqual(1, dbConfigStub.TimesCalled);
    Assert.AreEqual(storeValue, dbConfigStub.LastNameStored);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to add some testing functionality into our builds, and in order
All: I have a unit test that is testing functionality that requires an input
I am working with Visual Studio 2010 and it's integrated testing functionality. I have
I'm trying to get my head around the idea of testing functionality and minimizing
I'm testing QTreeView functionality right now, and i was amazed by one thing. It
I've just joined a team which has been working in a main-always mode for
I'd like to have a Cucumber feature testing the rememberable functionality of devise (a
I want to know the best way of testing data access functionality. I know
Is it possible to add the unit testing functionality (in Visual Studio 2010 Web
I've begun testing the In App Purchase functionality of my phone. It works great

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.