20
Testing Legacy Code Exciting approach to unit testing any ode, Keeping high quality & agility, and Creating wicked applications Eli Lopian, Founder Typemock

Triumph Over Legacy Code with Unit Testing

Embed Size (px)

DESCRIPTION

View the webinar: http://www.typemock.com/ppc/webinar/march-2014/on-demand.php Most developers believe unit testing is only applicable to greenfield projects or fairytales startups, but not as something that can be done in real-life situations, where a lot of legacy code need to be maintained. Reasons for not unit testing range from "setup is so big that the test is more complex than the code", to: "it's slow and takes ages to run", and: "it keeps on failing even though there is no bug", and to top it off: "it requires loads of maintenance". In this webinar, we will see that this is not true! Legacy code can be easily tested.

Citation preview

Page 1: Triumph Over Legacy Code with Unit Testing

Testing Legacy CodeExciting approach to unit testing any code,Keeping high quality & agility, and Creating wicked applications

Eli Lopian, Founder Typemock

Page 2: Triumph Over Legacy Code with Unit Testing

WHAT YOU WILL

LEARN

• Problems with legacy code • How isolation helps • How to unit test legacy code (demo)

Page 3: Triumph Over Legacy Code with Unit Testing

Legacy code is… Code without testsMichael Feathers

Page 4: Triumph Over Legacy Code with Unit Testing

Developers write millions of lines of legacy code

every day!

Page 5: Triumph Over Legacy Code with Unit Testing

Legacy Code Problems

• Code is tangled• Complex Tests• Death spiral• Catch-22

Page 6: Triumph Over Legacy Code with Unit Testing

Isolate it… (The most effective way to test legacy code )

• Fake Dependencies

• Change Code Behavior

• Verify Calls

Page 7: Triumph Over Legacy Code with Unit Testing

Fake Dependencies

var fakeProcess = Isolate.Fake.Instance<Process>();fakeProcess.MainModule.Site.Name

var fakeProcessHandle = Isolate.Fake.NextInstance<Process>(); var fakeAllProcessHandle = Isolate.Fake.AllInstances<Process>();

var fakeSimulator = Isolate.Fake.Dependencies<Simulator>();

Create a fake instance of a type:1

Signal that the next instance will be faked:2

Fake ALL the dependencies that are passed through the constructor:3

Page 8: Triumph Over Legacy Code with Unit Testing

Change Code Behavior

Isolate.WhenCalled(() => DateTime.Now).WillReturn(new DateTime(1977,5,25));

Isolate.WhenCalled(() => fake.MainModule.Site.Name).WillReturn("Typemock rocks");

var counter = new Counter ();Isolate.WhenCalled(() => counter.Increment()).WillReturn(1);Isolate.WhenCalled(() => counter.Increment()).WillReturn(2);

Change return value of a static method:1

Change return value of a chain of calls:

Change return value of a method from a real object:

2

3

Page 9: Triumph Over Legacy Code with Unit Testing

Verify Calls

Isolate.Verify.WasCalledWithAnyArguments(() => backoffice.Save());

Isolate.Verify.WasNotCalled(() => backoffice.Save());

int count = Isolate.Verify.GetTimesCalled(() => backoffice.Save());Assert.AreEqual(2, count);

The code called Save():

The code never calls Save():

The code calls the method 2 times:

1

2

3

Page 10: Triumph Over Legacy Code with Unit Testing
Page 11: Triumph Over Legacy Code with Unit Testing

Cannot call constructor because of dependencies

Page 12: Triumph Over Legacy Code with Unit Testing

Need method to return fake value

Page 13: Triumph Over Legacy Code with Unit Testing

Nothing to assert

Page 14: Triumph Over Legacy Code with Unit Testing

Fake an object to change its behavior.

Page 15: Triumph Over Legacy Code with Unit Testing

Need to change behavior of a private method

Page 16: Triumph Over Legacy Code with Unit Testing

Need to Fake an object that is created in the code

Page 17: Triumph Over Legacy Code with Unit Testing

• Isolate Dependencies

• Control Behavior

• Assert Calls

Fake It Till You Make It!

Page 18: Triumph Over Legacy Code with Unit Testing

PowerfulMocking

Visual Coverage SmartRunner

Typemock Isolator

Productivity*

* Complete Edition Only

Page 19: Triumph Over Legacy Code with Unit Testing

• Trial License• Online webinars• Cookbook• Team Demonstration

You can get more resources

Page 20: Triumph Over Legacy Code with Unit Testing

www.typemock.com

blog.typemock.com

@typemock

[email protected]

Questions?

Image creditsflickr@33909700@N02flickr@jamesukflickr@nikioflickr@duchampflickr@kalyan02flickr@clarkstonscampflicker@95786359@N05