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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:54:26+00:00 2026-06-10T16:54:26+00:00

I tries some Unit Testing and ran into a problem of testing methods which

  • 0

I tries some Unit Testing and ran into a problem of testing methods which use classes instances and not the interfaces. In that case I found that MS Moles can help me. But the it seems that they are not friendly with type casting situations. And I found no info and even no questions how to deal with this situation. Example:

public class ClassA
{
 public int Number {get {return 10;}}
}

public class ClassB
{
 public int Count1(ClassA arg) { return arg.Number; } 
 public int Count2(object arg) { return (arg as ClassA).Number; }
}

and while testing

var cl = new MolesUnitTesting.Moles.MClassA();
MolesUnitTesting.Moles.MClassA.AllInstances.NumberGet = t1 => 20;

The first Count works just fine and returns 20 but casting in the second returns Null. Is there any way to test such method without using interface and usual mocking? If there is other lib that can help me please provide me with it’s name.

  • 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-10T16:54:27+00:00Added an answer on June 10, 2026 at 4:54 pm

    Instead of mocking all instances of the ClassA type you can do the following:

    var target = new MClassA();
    
    target.NumberGet = () => 42;
    
    Assert.AreEqual(Count1(target), 42);
    Assert.AreEqual(Count2(target.Instance), 42);
    

    Notice that for the Count1 you can use the mole directly because it will be automatically converted to a ClassA instance. However, since Count2 receives an object you need to be explicit and passed the target.Instance which represents the moled ClassA instance.

    The reason Count1(target) works is because the generated mole class MClassA, which inherits from MoleBase<ClassA>, defines the following implicit operator:

    public static implicit operator ClassA(MoleBase<ClassA> mole) { // ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I converted a VS2008 solution that contains some unit testing projects to VS2010. After
I am running some unit test that persist documents into the MongoDb database. For
I was doing some unit testing on code that could throw a number of
I have a unit test which depends on some code that uses MEF. When
I'm working on a console application that tries to download some files from a
I m working on a console application that tries to download some files from
I am trying to get started with unit testing an app that uses Core
When developing for iPhone/iPad do you Do unit/integration/etc testing? What framework(s) do you use?
I want to start to do some unit testing however I am really confused
I'm using GMock to add some unit testing to our existing Java projects. We

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.