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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:14:30+00:00 2026-06-08T18:14:30+00:00

I have the following code: using System; using NUnit.Framework; using Rhino.Mocks; public class A

  • 0

I have the following code:

        using System;
        using NUnit.Framework;
        using Rhino.Mocks;

        public class A
        {
        }

        public class B
        {
        }

        public interface IStatementExecutor
        {
            void Exec(string statement);
        }

        public abstract class Foo<T>
        {
            private readonly IStatementExecutor _statementExecutor;
            private readonly string _targetSegment;

            protected Foo(IStatementExecutor statementExecutor, string targetSegment)
            {
                _statementExecutor = statementExecutor;
                _targetSegment = targetSegment;
            }

            public void Update(T item)
            {
                _statementExecutor.Exec("sp_" + _targetSegment + "Update");
            }
        }

        public class Bar : Foo<A>
        {
            public Bar(IStatementExecutor statementExecutor)
                : base(statementExecutor, "ATable")
            {
            }
        }

        public class Baz : Foo<B>
        {
            public Baz(IStatementExecutor statementExecutor)
                : base(statementExecutor, "BTable")
            {
            }
        }

        [TestFixture]
        public class Foo_Tests
        {
            [Test]
            public void Update_CallsStatementExecutorWithTableName()
            {
                const string tableName = "TestTable";
                var mockStatementExecutor = MockRepository.GenerateMock<IStatementExecutor>();
                mockStatementExecutor.Expect(m => m.Exec("sp_" + tableName + "Update"));
                var sut = MockRepository.GeneratePartialMock<Foo<A>>(mockStatementExecutor, tableName);
                var testModel = new A();


                sut.Update(testModel);

                mockStatementExecutor.AssertWasCalled(m => m.Exec("sp_" + tableName + "Update"));
            }
        }

I already have unit tests for the base class Foo<T>. Since the base class is already covered, I don’t want to write identical tests for the derived classes Bar and Baz.

The only thing I really care about in the derived classes is that the correct string target is passed to the base class.

I’m struggling on how to unit test this without breaking encapsulation of the derived classes or writing redundant unit tests.

So, the question is, how do I test that the correct value gets passed to the base class from the derived classes for the target parameter?

(If your answer is “use composition…”, please back it up with a code sample modified from above.

Thanks!

  • 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-08T18:14:31+00:00Added an answer on June 8, 2026 at 6:14 pm

    Think I’be more likely to test through the other methods on Bar and Baz, as you’d expect something bad to happen if you’d put ZTable in there instead of BTable

    You could add a method to Foo that would return what ever had been passed to it

    and then after creating the descendant call it and validate against the expected value.

    Or you could do something like

    public class Bar : Foo<A>     
    {         
      private static String _tableName = "ATable";
      public String  TableName {get {return _tableName;}}
      public Bar() : base(_tableName)         
      {         
      }     
    } 
    

    Then you could test testBar.TableName

    Another Option would be T was a struct or a class with a TableName property, then you wouldn’t need Bar and Baz descendants, just for this.

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

Sidebar

Related Questions

I have the following code : using System.Collections.Generic; public class Test { static void
I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
I have the following code block: - using System.ComponentModel.DataAnnotations; public class NewsItem { [RegularExpression(System.Configuration.ConfigurationSettings.AppSettings[UrlRegEx],
I have the following code: using System.Configuration; namespace test { public partial class MyService
I have the following C# code fragment: using System; class count { public static
I have the following code: using System; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; namespace MyTasks {
I have some questions about the following code: using System; namespace ConsoleApplication2 { public
I currently have the following code: <%@ WebService Language=C# Class=Notifications %> using System; using
i have the following code: using System; using System.IO; namespace Project { public partial
Given the following code: using System.Collections.Generic; static class Program { static void Main() {

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.