DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Creating Robust Tests with Isolator V7

Creating Robust Tests with Isolator V7

Dror Helper user avatar by
Dror Helper
·
Apr. 11, 12 · Interview
Like (0)
Save
Tweet
Share
3.10K Views

Join the DZone community and get the full member experience.

Join For Free
The problem with unit tests is that they keep on breaking…
Obviously that’s not entirely correct, nevertheless I had the pleasure of hearing the sentence above numerous times. It’s true – unit tests do tend to fail and we prefer that they fail only when a regression occurs – when something that used to work stopped working, because that’s the reason they’re there in the first place.
The problem is that there are several instances when a test stops compiling due to requirement change or refactoring. consider the following test code:
[Test]
public void Add_AddTowNumbersInCalculator_ReturnResult()
{
    var fakeDataAccess = Isolate.Fake.Instance<dataAccess>();
    Isolate.WhenCalled(() => fakeDataAccess.Save(null)).WillReturn(true);

    var customer = new Customer(fakeDataAccess);

    //...

    var result = customer.Save();

    Assert.That(result, Is.True);
}

Simple. Now what happens when we need to change the Customer c’tor? – let’s say that it was decided that every customer should have a valid address the developer implement that requirement – like this:
[Test]
public void Add_AddTowNumbersInCalculator_ReturnResult()
{
    var fakeDataAccess = Isolate.Fake.Instance<dataAccess>();
    var fakeAddress = Isolate.Fake.Instance<Address>();
    Isolate.WhenCalled(() => fakeDataAccess .Save(null)).WillReturn(true);

    var customer = new Customer(fakeAddress, dataAccess);

    //...

    var result = customer.Save();

    Assert.That(result, Is.True);
}

What if I already wrote a few dozen tests – what can I do?
  • Create overloaded c’tor one with parameter and one parameterless
  • Add a default value to the c’tor (.NET 4.0)
  • Write all of the tests again – from scratch
  • Delete your tests and never attempt unit testing your code ever again
  • Create a factory method to instantiate Customer at your tests
  • Quit your job and move to another country
All of the above are valid options (ok, some of these are valid options) they would work great for some of the scenarios. unfortunately most of the time usually you end up re-writing a lot of code each time such a change happen. Adding parameters that you usually do not care about and so you fake them or pass some default value over and over again.
In fact on my team this problem (needing to fix a lot of tests each time the c’tor signature change) was so severe that one of the developers took  it upon himself to create a class that creates object just for the test with all of the parameters faked.
Luckily there is another way: the new Isolator V7 that was released a few weeks ago and one new feature got me exited (no not the auto-test-runner) – it’s The new API calls: Isolate.Fake.Dependencies and Isolate.GetFake.
Using the new API I’ve fixed the test to work like this:
[Test]
public void Add_AddTowNumbersInCalculator_ReturnResult()
{
    var customer = Isolate.Fake.Dependencies<Customer>();

    var fakeDataAccess = Isolate.GetFake<DataAccess>(customer);
    Isolate.WhenCalled(() => fakeDataAccess.Save(null)).WillReturn(true);
    //...

    var result = customer.Save();

    Assert.That(result, Is.True);
}
I just tell Isolator to fake all dependencies and “grab” the only one that interest me at this test and change it’s behavior – simple.
And now my test won’t break due to c’tor changes.
unit test

Published at DZone with permission of Dror Helper, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Recent Amazing AI Advancements
  • Web Testing Tutorial: Comprehensive Guide With Best Practices
  • How to Assess the Technical Skills of a Software Development Partner
  • Effective Jira Test Management

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: