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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:29:49+00:00 2026-06-13T06:29:49+00:00

[Test] public void testMultiplication() { var five=new Dollar(5); Assert.AreEqual(new Dollar(10), five.times(2)); Assert.AreEqual(new Dollar(15), five.times(3));

  • 0
[Test]
public void testMultiplication()
{
    var five=new Dollar(5);
    Assert.AreEqual(new Dollar(10), five.times(2));
    Assert.AreEqual(new Dollar(15), five.times(3));
}

Dollar class

public class Dollar
{
    private int amount;

    public Dollar(int amount)
    {
        this.amount = amount;
    }

    public Dollar times(int multiplier)
    {
        return new Dollar(amount * multiplier);
    }

    public bool equals(Object theObject)
    {
       Dollar dollar = (Dollar) theObject;

       return amount == dollar.amount;
    }
}

On line Assert.AreEqual(new Dollar(10), five.times(2)); test fail with error:

Expected: TDDbooks.Dollar

But was: TDDbooks.Dollar

  • 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-13T06:29:50+00:00Added an answer on June 13, 2026 at 6:29 am

    NUnit displays string representation of objects. In order to have handy output, you should override ToString method of Dollar class:

    public override string ToString()
    {
       return "$" + amount;
    }
    

    Now output will be like:

    Expected: $10 
    But was: $10
    

    Next problem is dollars comparison. NUnit compare objects by calling Equals method (not equals, but Equals. Kent Beck uses Java in his examples. In C# we have Pascal naming for methods). Default implementation of Equals method return true if objects have same reference. But in Times method you create new instance of Dollar class. In order to fix that, you should change Equals method implementation to compare amount field.

    public override bool Equals(object obj)
    { 
      Dollar other = obj as Dollar;
      if (other == null)
        return false;  
    
      return amount == other.amount;
    }
    

    Also notice, that you should use override keyword for overriding base class functionality. And one more thing – when you are overriding Equals functionality, you should override GetHashCode method. In your case it’s OK to have something like:

    public override int GetHashCode()
    {
      return amount.GetHashCode();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class: class test { public: void print(); private: int x;
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
using NUF = NUnit.Framework; [NUF.Test]public void DifferentCastingTest() { NUF.Assert.That((int)0.499999D, NUF.Is.EqualTo(0)); NUF.Assert.That((int)0.500000D, NUF.Is.EqualTo(0)); // !!!
Say I have this test: [Test] public void SomeTest() { var message = new
public class test { public static final int nThreads = 2; public static void
public class Test{ public void Test(String name){}; public static void main() { Test t=new
@Autowired private Validator validator; @Test public void testValidateMethodOfPaymentBadCreditCard() { final MethodOfPayment mop = new
I have the following test: @Test(expected=ArithmeticException.class) public void divideByZero() { int n = 2
I Have the following test: [Test] public void Add_New_Group_Should_Return_StatusCode_Created_And_A_Header_Location_To_The_New_Group() { var newGroup = new
I have the following test: [Test] public void VerifyThat_WhenInitializingTheLoggingInterceptionFacility_TheLoggingInterceptorIsAdded() { var kernel = new

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.