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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:53:42+00:00 2026-05-22T23:53:42+00:00

Possible Duplicate: Hard-Coded Mock Objects vs Mocking Framework I think I’m finally beginning to

  • 0

Possible Duplicate:
Hard-Coded Mock Objects vs Mocking Framework

I think I’m finally beginning to understand what unit tests are intended to solve, but am still having trouble implementing all of the details. I’ve come to the conclusion that I may require a “mock” (and I use this term lightly since I’m not sure I need an entire framework like Moq) object to get the job done.

As an example of the issue I have been running in to, consider an implementation of the Repository Pattern (or similar). As I currently understand, I would need (at a minimum) tests for each of the Add(), Get(), and Remove() class methods. This is fine, except suppose I want to test how the Add() method handles null references. In this case, would I merely define a simple class within the test project and set an instance of this to null within the appropriate unit test?

Example Unit Test (Illustration):

[TestMethod]
public void TestAdd_Null()
{
    IRepository<MockObject> repository = (IRepository<MockObject>)(new Repository<MockObject>());
    MockObject testObject = null;

    repository.Add(testObject);

    Assert.IsNotNull(repository.Entity);
}

// I'm thinking I should implement something like this exclusively within the Test project.
// Is this reasonable? Or should I be looking into something else?
internal class MockObject
{
    public String Name { get; set; }
}
  • 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-22T23:53:43+00:00Added an answer on May 22, 2026 at 11:53 pm

    I’d go as far as to say that software testing, is like The Matrix. No one can be told what software testing is, you have to see it for yourself. Most non-believers haven’t given testing a fair chance and never tried doing some testing. Welcome to the club!

    The tricky thing about testing though is that it’s quite hard to write tests, it’s also challenging sometimes to write testable code. There are however many great tools and techniques out there today that you should invest in to become a better software engineer.

    Mocks provide dummies that you’d otherwise have to write yourself, mocks are convenient. Your repository in this case is not an actual persistence service in this case it simply provides a contract to test with. If adding a null reference isn’t supposed to be possible you should have a test cases which expect such an operation to fail. I believe this goes without saying but it’s important to test this to.

    A library such as Moq could be very helpful here because a dummy repository does nothing and you actually want an assertion to take place here. I don’t see how a property Entity after something was added is needed here (other than to maybe make it easier to write the test). But I also think that it’s the wrong way to assert the expected behavior.

    To some extent what you are doing is actually referred to as integration testing because you are not testing an isolated unit your using a mock factory to test the interaction between two components. These kinds of tests are very important but also hard to deal with if they weren’t designed to be testable. And this is why we have things such as dependency injection.

    Your question doesn’t really require a specific answer, but you’re on the right track and I hope this will give you some insight and additional confidence when doing software testing.

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

Sidebar

Related Questions

Possible Duplicate: How should I unit test threaded code? The classical unit testing is
Possible Duplicate: Entity Framework Include() strongly typed Ok, I've got an entity framework query
Possible Duplicate: Regexp recognition of email address hard? Hi, I would like to implement
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: How do you send email from a Java app using Gmail? How

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.