Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,736 members, 7,809,819 topics. Date: Friday, 26 April 2024 at 03:28 PM

Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD (5312 Views)

[php Tutorial] - Building A Rest Api / Laravel Tutorial: Building An E-commerce Web App - Part1 / Creating UWP Application In C# Or C++ (xaml) (2) (3) (4)

(1) (2) (Reply) (Go Down)

Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 11:25pm On Aug 01, 2015
TOPIC: Tutorial: Building a Simple Fraction Arithmetic Program in C# using TDD

Hi, Everyone. I’ve been learning TDD (Test-Driven Development) for a (longggg) while now and I’m revisiting it now for good. I’d like to have firm crisp of it and implement it in my existing projects and new ones.
So I’m writing this tutorial to aid my knowledge.
Note: This is my first tutorial on any forum. Expect Typos (Lolz)
I’m Building a Simple Fraction Arithmetic Program in C# using Test-Driven Development Method.
Pls, Creative , criticisms and corrections are strongly welcomed.
Peace !!!!
Cc: dhtml18 pcguru1 Seun lordZOUGA
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by Esdb3: 11:28pm On Aug 01, 2015
I will soon understand the gibberish u described.
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 11:31pm On Aug 01, 2015
Esdb3:
I will soon understand the gibberish u described.
yea.. Learn with me !!!
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 12:47am On Aug 02, 2015
POST 2:
What’s Test-driven development?
TDD , is a disciplined programming technique built around a simple idea:

no production code is written before there’s a failing test that justifies the existence of that code.

That’s why it’s sometimes referred to as test-first programming.

It’s more than that; by writing a test first, you drive your production code toward a design called for by the test.
This has the following desirable consequences:

Code becomes usable —The design and API of your code are suitable for the kind of
scenarios you’re going to use it for.

Code becomes lean —The production code only implements what’s required by
the scenarios it’s used for.
[img] [/img]





The Fundamental Test Life Cycle

[size=30pt] The Golden Rule of Test-Driven Development [/size]
[size=25pt]
Never write new functionality without a failing test. [/size]

Won’t be boring you with the jargons of TDD. I’d advise you google Books on it to Learn more.
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 11:46am On Aug 02, 2015
PROGRAM REQUIREMENTS
Our Program is to accept as input integers representing values that compose two fractions (Mixed or Ordinary Fraction) and Perform Arithmetic Operations on them.
The operations include:
Additions
Subtractions
Multiplication
Division
Comparisons (Equals and Not Equals)

Fractions can also be:
Incremented
Decremented
Outputted in Human Readable form
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 11:51am On Aug 02, 2015
UNDERSTANDING THE PROBLEM : FRACTION ARITHMETIC

Before we jump unto our systems and get the test running and the codes rolling we need to understand the problem

So what is a fraction ?
According to Wiki, hmmm..blah blah

Types:
Mixed Fractions and Ordinary Fraction
[[img]http:///a/img633/1702/Z3CiYM.png[/img]

That understood. How are Arithmetic Operations Performed between two fractions ?
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 12:03pm On Aug 02, 2015
Updated: 10/08/2015 11:43
UNDERSTANDING THE PROBLEM : ARITHMETIC OPERATIONS BETWEEN ORDINARY FRACTIONS
ADDITION, SUBSTRACTION, AND MULTIPLICATION OPERATIONS

[img]http:///a/img673/7564/rV1occ.png[/img]

From the Above we can see Similarities between them. Understanding these would aid coding.
Division Operations is a little different

[img]http:///a/img661/7716/5R1vDr.png[/img]

That Understood.
We know all we need to begin the Development.

So Let’s Get CODING !!!

Question: But Oga Jacobu , We’ve not Discussed about mixed fractions how can we begin coding?

Answer: According to Agile Practices, You don’t need to know all about your system before you get coding (Water Fall). We just need to know part of the system that is most needed currently and independent of other areas of our project or system not yet implemented. We’d treat that in other next iteration so we log that feature in our iterations backlog .

That Said…. LET’S GET THE TESTS ROLLING
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 12:11pm On Aug 02, 2015
Contributions pls?

1 Like

Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 3:25pm On Aug 02, 2015
PROJECT SETUP
For this Tutorial, You’d need Microsoft Visual Studio (Express, Professional, Ultimate ) installed on your system. Once installed , with Visual C# included, we can proceed.
Our Solution Layout

We’d create three separate projects in our solution.
1) FractionConsoleApp : Contains our Console App
2) FractionLibrary : Contains our App Classes
3) FractionTest : Contains our Test Classes

So, to begin, Launch Visual Studio.

1) Select File -> New -> Project

[img]http:///a/img537/5614/lCECS0.jpg[/img]

Select Visual C# from Templates in the Left Pane; Choose Console Application in the Center Pane. For Project Name: FractionConsoleApp and Solution name: FractionArithmeticApp . Click Ok

[img]http:///a/img910/7241/FMXTx6.jpg[/img]

Our newly Created Solution
[img]http:///a/img901/7421/OeR4lw.jpg[/img]

2) Next, Right Click on the Solution Name -> Add -> Click New Project

[img]http:///a/img673/1190/otu4r1.jpg[/img]

3) Choose Class Library in the Center pane; For Project Name: FractionLibrary .Click Ok

[img]http:///a/img913/2198/QGyT6z.jpg[/img]

Repeat Step (4), Select Test in Left pane and Select Unit Test Project in the Center pane; For Project Name: FractionTest .Click Ok

[img]http:///a/img901/9225/J6MDMM.jpg[/img]

4) Our Solution’s Structure should look like this

[img]http:///a/img673/2589/kF2TFl.jpg[/img]
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 6:32am On Aug 03, 2015
Updates coming soon
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by valonsoft(m): 10:00pm On Aug 03, 2015
Good work, Bro
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 7:51am On Aug 04, 2015
valonsoft:
Good work, Bro
Thanks...
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by Pffft: 8:21am On Aug 04, 2015
Following.... Lets see your first Test
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 8:58am On Aug 04, 2015
Pffft:
Following.... Lets see your first Test
Thanks for following. hope make the much needed contribution I require.

I'd post the major tests today once I get my internet sub fixed..
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 3:53pm On Aug 04, 2015
BEGINNING TESTS

Now our project is ready for Development.

Question: What should we test?
Test (To Me) should be done on behaviours (methods) in our apps that returns a result that of value for us to pass our acceptance test.

Going to Our Requirements, we to create Fractions that can be used for Arithmetic operations.

Why not begin with testing for proper creation of ordinary Fraction Objects?
Let’s Go back to Visual Studio.
1) Rename the Generated Class UnitTest1.cs in our Unit Test Project to FractionArithmeticTests.cs and the Class name to FractionArithmeticTests (if not Refactor-ed automatically)
2) Open FractionArithmeticTests.cs and add a new method named testOrdinaryFractionCreation with [TestMethod] Decorator
Our Class, FractionArithmeticTests.cs , Should Look like this

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FractionTest
{
[TestClass]
public class FractionArithmeticTests
{
[TestMethod]
public void testOrdinaryFractionCreation()
{
}
}
}







“ When we write a test, we imagine the perfect interface for our operation. We are telling ourselves a story about how the operation will look from the outside. Our story won’t always come true, but better to start from the best possible API and work backwards than to make things complicated, ugly, and “realistic” from the get go.”

When writing test, we write how we want our class interface to look like without necessarily creating them yet.
Going back to Code.

[TestMethod]
public void testOrdinaryFractionCreation()
{
Fraction firstFraction = new Fraction();
Assert.AreEqual(0, firstFraction.Numerator);
Assert.AreEqual(0, firstFraction.Denominator);
}


[img]http:///a/img661/7277/37fGW0.jpg[/img]

Question: But Wait ooo. Oga Jacobu. Are you ok? How can we test a Class that doesn’t exist yet?
Answer: That’s the plan !!! Welcome to TDD. We’re writing how our Class objects interface should look like before writing the actual Class. We always write Tests at first that won’t compile (Notice the Red wiggly line under Fraction Class). Then we make it compile. After which our test library Asserts it False then we make it Pass !!!..
Our Next Step now is to make our App Compile by Creating the Class and implementing the property.
1) Right Click On FractionLibrary in the Solution Explorer -> Add -> Click Class

[img]http:///a/img912/9877/bIjhou.jpg[/img]


2) Name the Class Fraction.cs
[img]http:///a/img910/696/avIp7z.jpg[/img]
We have (Note Make the Class Public)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FractionLibrary
{
public class Fraction
{
}
}


3) Next You’d Observe the Red wiggly isn’t gone yet. This is because our Fraction Class is Another Project and namespace. We need to add a Reference to the FractionLibrary Project in our Fraction Project.
4) Right Click On References under FractionTest project in the Solution Explorer -> Click Add Reference

[img]http:///a/img540/3117/lJm6qK.jpg[/img]
5) Check FractionLibrary in the list. Click Ok
[img]http:///a/img673/8473/i6Kz3a.jpg[/img]
6) Update FractionArthmeticTests.cs by adding using FractionLibrary;
Our FractionArthmeticTests.cs Class now

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using FractionLibrary;

namespace FractionTest
{
[TestClass]
public class FractionArithmeticTests
{
[TestMethod]
public void testOrdinaryFractionCreation()
{
Fraction firstFraction = new Fraction();

Assert.AreEqual(0, firstFraction.Numerator);
Assert.AreEqual(0, firstFraction.Denominator);
}
}
}


But We Still Can’t Compile Yet Because the Two Properties Numerator and Denominator are not declared in our class.
[img]http:///a/img537/2171/6Lpl68.jpg[/img]
7) Let’s Add them …. Update Fraction.cs .. Note you can use Visual Studio’s Generate Stub to Quick Add missing methods or properties… Click on the Property, Hover the Little blue line under it then click Generate Stub
[img]http:///a/img633/5951/YguFhq.jpg[/img]
Repeat for Denominator too.

….
public class Fraction
{

public int Numerator { get; set; }

public int Denominator { get; set; }
}

cool Our Project Should build in peace now !!!... Right Click Anywhere on the FractionArthmeticTests Code ..A Dialog Appears, Click On Run Tests !!!
[img]http:///a/img537/4129/wU1Qiv.jpg[/img]
9) Congratulations !!! We’ve just Passed a Test
[img]http:///a/img910/1765/RBAA9O.jpg[/img]
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 4:04pm On Aug 04, 2015
Fixed Wrong Images placements
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 4:19pm On Aug 04, 2015
[sup][/sup] BEGINNING TESTS: FURTHER TESTING ORDINARY FRACTION CREATION
Test Cycle
1. Add a little test
2. Run all tests and fail
3. Make a little change
4. Run the tests and succeed
5. Refactor to remove duplication
Now, Test Further …
Let’s Test our Super Fraction class by creating a new Fraction with Numerator of 5 and Denominator of 10 These we want to pass to the Fraction Class Constructor.
Update testOrdinaryFractionCreation method

public void testOrdinaryFractionCreation()
{
Fraction firstFraction = new Fraction();

Assert.AreEqual(0, firstFraction.Numerator);
Assert.AreEqual(0, firstFraction.Denominator);

firstFraction = new Fraction(5,10);
Assert.AreEqual(5, firstFraction.Numerator);
Assert.AreEqual(6, firstFraction.Denominator);
}

Notice the Red wiggly line.
[img]http:///a/img661/2473/CWO9ag.jpg[/img]
Our Tests won’t run until with fix or implement the construct.
Click on the Fraction(5,10) , Hover the Little blue line under it then click Generate Stub.. Amend the Generated Stub so that we have

public class Fraction
{

public Fraction(int numerator, int denominator)
{
// TODO: Complete member initialization
this.Numerator = numerator;
this.Denominator = denominator;
}

public int Numerator { get; set; }

public int Denominator { get; set; }
}

But Notice we still have Another Error .. Red wiggly line under new Fraction()

[img]http:///a/img901/472/FxGxuQ.jpg[/img]

With Error Message
[img]http:///a/img673/3497/CAlB6A.jpg[/img]
This is because of the new Constructor we’ve just create. If a class has no constructor , “C#” creates an empty constructor for us. but if any constructor is declared, it does not create it. In our case, we need to create another constructor that accepts no value. Let’s Go There !!!
So, like before, Click on the Fraction() , Hover the Little blue line under it then click Generate Stub..
Our Fraction.cs class now

public class Fraction
{

public Fraction(int numerator, int denominator)
{
// TODO: Complete member initialization
this.Numerator = numerator;
this.Denominator = denominator;
}

public Fraction()
{
// TODO: Complete member initialization
}

public int Numerator { get; set; }

public int Denominator { get; set; }
}

Visual Studio Should bug you again.
Right Click Anywhere on the FractionArthmeticTests Code , A Dialog Appears, Click On Run Tests !!! Our code Should work and build successfully passing the test.
Note: We haven’t had any failed test yet.. well that’s because C# has been helpful defaulting new properties to 0 (in the case of creating new empty Fraction) and we’ve not really done much.
You can test Further with new values

public class FractionArithmeticTests
{
[TestMethod]
public void testOrdinaryFractionCreation()
{
Fraction firstFraction = new Fraction();

Assert.AreEqual(0, firstFraction.Numerator);
Assert.AreEqual(0, firstFraction.Denominator);

firstFraction = new Fraction(5,10);
Assert.AreEqual(5, firstFraction.Numerator);
Assert.AreEqual(10, firstFraction.Denominator);

firstFraction = new Fraction(10, 5);
Assert.AreEqual(10, firstFraction.Numerator);
Assert.AreEqual(5, firstFraction.Denominator);
}
}
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 4:29pm On Aug 04, 2015
Viewing this topic: lordZOUGA(m) ... hi boss grin grin .. waiting to hear from you
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 4:44pm On Aug 04, 2015
BEGINNING TESTS: FURTHER TESTING ORDINARY FRACTION CREATION 2
So, far how test value for creation of the Ordinary Fraction objects has been quite predictable.
What if a curious user inputs 0 (zero) for either Numerator or Denominator via our constructor?
While this can be avoid by checking the input value before using them to create Fraction objects. We (I) don’t want the class to be “DEFENSELESS”. We to throw an Exception of invalid input supplied.
So, Let Amend our FractionArithmeticTests class with this

[TestClass]
public class FractionArithmeticTests
{
[TestMethod]
public void testOrdinaryFractionCreation()
{
Fraction firstFraction = new Fraction();

Assert.AreEqual(0, firstFraction.Numerator);
Assert.AreEqual(0, firstFraction.Denominator);

firstFraction = new Fraction(5,10);
Assert.AreEqual(5, firstFraction.Numerator);
Assert.AreEqual(10, firstFraction.Denominator);

firstFraction = new Fraction(10, 5);
Assert.AreEqual(10, firstFraction.Numerator);
Assert.AreEqual(5, firstFraction.Denominator);

try{

firstFraction = new Fraction(0, 1);
Assert.Fail("Shouldn't be Able to Create a Fraction with Zero Numerator"wink;

}
catch (ArgumentOutOfRangeException ex)
{

}

}
}

In the Above Test, We’re trying to Assert that Fraction class with a numerator value of 0 is not instantiate-able. We want our Fraction class to throw an Exception ArgumentOutOfRangeException . if the Object throws the Exception (ArgumentOutOfRangeException) for firstFraction = new Fraction(0, 1); then is caught, nothing is done, skips Assert.Fail("Shouldn't be Able to Create a Fraction with Zero Numerator"wink; , and passes the test. Otherwise, If Fails.
Let’s Try and run our Test Again.. (Fingers Crossed)… 
Right Click Anywhere on the FractionArthmeticTests Code ..A Dialog Appears, Click On Run Tests !!!


Congratulations    !!! Our First Failed Test
[img]http:///a/img661/1235/weV1vE.jpg[/img]

Our test failed as expected with the Message “Shouldn't be able to Create a Fraction with Zero Numerator” . Notice the Red Bar when our Test Fails and also Green when our test Pass.
These is the TDD Life Cycle

[img]http:///a/img537/6002/mhIrAv.jpg[/img]

Red: Make your test Fail by writing a failing test
Green: Make you test Pass by implementing the needful
Refactor: Make the Code Right by remove redundant codes or duplicates.
Our Next Step is to make our Test by implementing validation before values are assigned.
We do it in two ways..
1) Validating the values in the constructor before assigning it to properties
2) Validating the values while setting value in the Property
I’d go with the first, Why ?, Because I choose to …. LOLz
Ok … Lets Edit Our Fraction Class and Implement the validation of Numerator.

……
public Fraction(int numerator, int denominator)
{
// TODO: Complete member initialization
if (numerator == 0) { throw new ArgumentOutOfRangeException("Numerator Can't be Zero"wink; }
this.Numerator = numerator;
this.Denominator = denominator;
}

That Done, Let’s Run Our Test Again.

Yes !!!, Test Passed !!!
[img]http:///a/img673/6189/X7GPrz.jpg[/img]


Next, Let’s Test That Denominator can be Zero too..
Add a new try..catch Block in the testOrdinaryFractionCreation Method in FractionArithmeticTests after the previous try..catch Block

….
try
{

firstFraction = new Fraction(0, 1);
Assert.Fail("Shouldn't be able to Create a Fraction with Zero Denominator"wink;
}
catch (ArgumentOutOfRangeException ex) { }

Let’s run our Test Again..
Right Click Anywhere on the FractionArthmeticTests Code ..A Dialog Appears, Click On Run Tests !!!
Congratulobia !!!. Our Test Failed Again with the message "Shouldn't be able to Create a Fraction with Zero Denominator"

[img]http:///a/img537/7495/GADsEo.jpg[/img]

Now Let’s make our test pass by checking the denominator before assignment in the constructor
Update
Ok … Lets Edit Our Fraction Class and Implement the validation of Denominator.

……
public Fraction(int numerator, int denominator)
{
// TODO: Complete member initialization
if (numerator == 0) { throw new ArgumentOutOfRangeException("Numerator Can't be Zero"wink; }
if (denominator == 0) { throw new ArgumentOutOfRangeException("Denominator Can't be Zero"wink; }

this.Numerator = numerator;
this.Denominator = denominator;
}

Run the Test Again.
Yes, (Dancing Shokiti Bobo),(Dtml18) our test passed !!!.

[img]http:///a/img911/6486/rIbJif.jpg[/img]

That’s about it, for now, with Ordinary Fraction Creation Test. Pls let me know other tests that can be performed.
Fraction.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FractionLibrary
{
public class Fraction
{

public Fraction(int numerator, int denominator)
{
// TODO: Complete member initialization
if (numerator == 0) { throw new ArgumentOutOfRangeException("Numerator Can't be Zero"wink; }
if (denominator == 0) { throw new ArgumentOutOfRangeException("Denominator Can't be Zero"wink; }

this.Numerator = numerator;
this.Denominator = denominator;
}

public Fraction()
{
// TODO: Complete member initialization
}

public int Numerator
{
get;
set;
}

public int Denominator
{
get;
set;

}
}
}



FractionArithmeticTests.cs

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using FractionLibrary;

namespace FractionTest
{
[TestClass]
public class FractionArithmeticTests
{
[TestMethod]
public void testOrdinaryFractionCreation()
{
Fraction firstFraction = new Fraction();

Assert.AreEqual(0, firstFraction.Numerator);
Assert.AreEqual(0, firstFraction.Denominator);

firstFraction = new Fraction(5,10);
Assert.AreEqual(5, firstFraction.Numerator);
Assert.AreEqual(10, firstFraction.Denominator);

firstFraction = new Fraction(10, 5);
Assert.AreEqual(10, firstFraction.Numerator);
Assert.AreEqual(5, firstFraction.Denominator);

try{

firstFraction = new Fraction(0, 1);
Assert.Fail("Shouldn't be able to Create a Fraction with Zero Numerator"wink;
}
catch (ArgumentOutOfRangeException ex){}

try
{

firstFraction = new Fraction(1, 0);
Assert.Fail("Shouldn't be able to Create a Fraction with Zero Denominator"wink;
}
catch (ArgumentOutOfRangeException ex) { }


}
}
}
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 4:58pm On Aug 04, 2015
Updated: 10/08/2015 11:43
BEGINNING TESTS: TESTING ADDITION OF ORDINARY FRACTIONS
For Addition Tests, Let’s Create a new Test Method Named testOrdinaryFractionsAdditions

[TestMethod]
public void testOrdinaryFractionsAdditions()
{

}

There are two ways we can implement this in C#
1) Using method
2) Using Operator Overloading
Hmmmm.. Lets Implement Both in Parallel !!!.
Rename testOrdinaryFractionsAdditions to testOrdinaryFractionsAdditionsUsingMethod and Add a new Test Method Named testOrdinaryFractionsAdditionsUsingOpOverloading
Now let’s write a test for Addition Using Method.Let’s write how the interface would look like then make it compile

[TestMethod]
public void testOrdinaryFractionsAdditionsUsingMethod()
{
Fraction firstFraction = new Fraction(5,10);
Fraction secondFraction = new Fraction(3,9);
Fraction result = firstFraction.Add(secondFraction);

Assert.AreEqual(75, result.Numerator);
Assert.AreEqual(90, result.Denominator);


}




Notice the Red wiggly Line under firstFraction.Add .

[img]http:///a/img912/2290/YSFEGL.jpg[/img]

That’s because it’s not yet Implemented. Click on the Add method, Hover the Little blue line under it then click Generate Method Stub.
Update the generated Add Method in Fraction.cs to

public Fraction Add(Fraction secondFraction)
{
return new Fraction(75, 90);
}

Question: Oga Jacobu, Why did you just return a Fraction object like that without any calculation or something?
Answer: That’s TDD for you, you write test progressively in simple baby steps to just pass the current test. Then later we write another test that would prompt the correction of the logic. Simple huh ?
Now, Let’s run the test and ensure we pass it .

Yup !!! Test passed

Next, Let’s Update testOrdinaryFractionsAdditionsUsingMethod to test for Another Pair of Fractions.

[TestMethod]
public void testOrdinaryFractionsAdditionsUsingMethod()
{
Fraction firstFraction = new Fraction(5,10);
Fraction secondFraction = new Fraction(3,9);
Fraction result = firstFraction.Add(secondFraction);

Assert.AreEqual(75, result.Numerator);
Assert.AreEqual(90, result.Denominator);

firstFraction = new Fraction(6, 10);
secondFraction = new Fraction(7, 20);
result = firstFraction.Add(secondFraction);

Assert.AreEqual(50, result.Numerator);
Assert.AreEqual(200, result.Denominator);


}



Run the Test Again.. It should fail.
Yep, It does. With Message : Expected<50> .Actual<75>
[img]http:///a/img537/9666/usAS97.jpg[/img]

It Failed because, for our previous test to pass, we just returned a new Fraction object in the Add Method with the values we expected.
Now, Let’s Implement the Calculation from the Logic Discussed Earlier.
Update Add Method to

public Fraction Add(Fraction secondFraction)
{
int newNumerator = (this.Numerator * secondFraction.Denominator) +
(secondFraction.Numerator * this.Denominator);
int newDenominator = this.Denominator * secondFraction.Denominator;
return new Fraction(newNumerator, newDenominator);
}

Save and Run the Test File.

Passed !!!
[img]http:///a/img913/192/Ee0EqJ.jpg[/img]
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 4:59pm On Aug 04, 2015
UPDATES LOADED... LOADING MORE LATER.. but i need your responses
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by Pffft: 4:59pm On Aug 04, 2015
Nice... I'm not a fan of too many Asserts in a method sha, also don't like d idea of try catch. I think it should have its own test with an attribute as against a try catch. Just my thoughts, anyway there are many roads into a market. Keep it coming bro

1 Like

Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 6:33pm On Aug 04, 2015
Pffft:
Nice... I'm not a fan of too many Asserts in a method sha, also don't like d idea of try catch. I think it should have its own test with an attribute as against a try catch. Just my thoughts, anyway there are many roads into a market. Keep it coming bro
valid points.... I'm not a fan of try ...catch too..I've thought about it while writing the tutorial and thanks for pointing it out again.. but the test is actually intensional and It's meant to be discussed on later....wink
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 4:36pm On Aug 05, 2015
updates loading
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by Nobody: 7:09pm On Aug 05, 2015
GOOD job. Pyjac.... I feel you.

1 Like

Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by Bros1: 7:29pm On Aug 05, 2015
Good job.
I will advise you also put it up on codeproject.com for a wider reach.

Thanks
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 11:34pm On Aug 05, 2015
ibrodex:
GOOD job. Pyjac.... I feel you.
thanks bro !!!...same ibrodex from kp
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 11:35pm On Aug 05, 2015
Bros1:
Good job. I will advise you also put it up on codeproject.com for a wider reach.
Thanks
thanks.. I would
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by afolwalex20(m): 10:31am On Aug 06, 2015
Na we dey here bro

1 Like

Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by Nobody: 6:17pm On Aug 06, 2015
Uhm, this is a very explicit tutorial and i love it. But the problem is that I dont really understand it as it is not written in my native dialect
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 8:08pm On Aug 06, 2015
dhtml18:
Uhm, this is a very explicit tutorial and i love it. But the problem is that I dont really understand it as it is not written in my native dialect
thanks...hmmmm...native dialect..lolx
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by dhtml(m): 9:10pm On Aug 06, 2015
jacob05:
thanks...hmmmm...native dialect..lolx
Err yoruba language i think - i am speaking for dhtml18 the troll who has been banned for trolling (as usual).

(1) (2) (Reply)

My Software Development Journey / Website Programmer Wanted / POS Machine Configuration

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 78
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.