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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:21:55+00:00 2026-05-15T08:21:55+00:00

I am embarking upon my first journey of test driven development in C#. To

  • 0

I am embarking upon my first journey of test driven development in C#. To get started I’m using MSTest and Rhino.Mocks. I am attempting to write my first unit tests against my ICustomerRepository. It seems tedious to new up a Customer for each test method. In ruby-on-rails I’d create a seed file and load the customer for each test. It seems logical that I could put this boiler plate Customer into a property of the test class but then I would run the risk of it being modified. What are my options for simplifying this code?

[TestMethod]
public class CustomerTests : TestClassBase
{
    [TestMethod]
    public void CanGetCustomerById()
    {
        // arrange
        var customer = new Customer()
        {
            CustId = 5,
            DifId = "55",
            CustLookupName = "The Dude",
            LoginList = new[] {
                new Login { LoginCustId = 5, LoginName = "tdude" } }
        };

        var repository = Stub<ICustomerRepository>();

        // act
        repository.Stub(rep => rep.GetById(5)).Return(customer);

        // assert
        Assert.AreEqual(customer, repository.GetById(5));
    }

    [TestMethod]
    public void CanGetCustomerByDifId()
    {
        // arrange
        var customer = new Customer()
        {
            CustId = 5,
            DifId = "55",
            CustLookupName = "The Dude",
            LoginList = new[] {
                new Login { LoginCustId = 5, LoginName = "tdude" } }
        };

        var repository = Stub<ICustomerRepository>();

        // act
        repository.Stub(rep => rep.GetCustomerByDifID("55")).Return(customer);

        // assert
        Assert.AreEqual(customer, repository.GetCustomerByDifID("55"));
    }

    [TestMethod]
    public void CanGetCustomerByLogin()
    {
        // arrange
        var customer = new Customer()
        {
            CustId = 5,
            DifId = "55",
            CustLookupName = "The Dude",
            LoginList = new[] {
                new Login { LoginCustId = 5, LoginName = "tdude" } }
        };

        var repository = Stub<ICustomerRepository>();

        // act
        repository.Stub(rep =>
            rep.GetCustomerByLogin("tdude")).Return(customer);

        // assert
        Assert.AreEqual(customer, repository.GetCustomerByLogin("tdude"));
    }
}

Test Base Class

public class TestClassBase
{
    protected T Stub<T>() where T : class
    {
        return MockRepository.GenerateStub<T>();
    }
}

ICustomerRepository and IRepository

public interface ICustomerRepository : IRepository<Customer>
{
    IList<Customer> FindCustomers(string q);
    Customer GetCustomerByDifID(string difId);
    Customer GetCustomerByLogin(string loginName);
}

public interface IRepository<T>
{
    void Save(T entity);
    void Save(List<T> entity);
    bool Save(T entity, out string message);
    void Delete(T entity);
    T GetById(int id);
    ICollection<T> FindAll();
}
  • 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-15T08:21:56+00:00Added an answer on May 15, 2026 at 8:21 am

    I would extract the code to create the customer into a method. You can call that from your test methods, and if you name it something like SetUpCustomerForRepository then it will provide some documentation about what you are doing.

    Here is an example based on your sample:

     private Customer SetUpCustomerForRepository()
     {
        return new Customer()
        {
            CustId = 5,
            DifId = "55",
            CustLookupName = "The Dude",
            LoginList = new[] {
                new Login { LoginCustId = 5, LoginName = "tdude" } }
        };
     }
    

    You could also call it from the test set up method, but I actually prefer to do it in the test method so that people looking at the test know what is being set up for the test.

    As for your concern about it being modified….if that happens, then your tests will fail, and you’ll know about it.

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

Sidebar

Related Questions

I am embarking upon my first ASP.NET MVC project and I would like to
Just embarking on using a test framework for writing unit tests and also the
I am embarking upon a NLP project for sentiment analysis. I have successfully installed
I am embarking on some learning and I want to write my own syntax
I planned on using the jquery charting library flot; before embarking on this adventure
We are embarking on our first Silverlight project, coming from WPF. It's a relatively
I'm toying with the idea of embarking on a cloud-based client/server spare-time project using
Looking to see if something is technically possible before embarking on its development. The
I'm embarking on my first foray into mobile site design - I've set up
I am just embarking on my first large-scale refactor, and need to split an

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.