Triumph Over Legacy Code with Unit Testing
-
Upload
typemock -
Category
Technology
-
view
782 -
download
3
description
Transcript of 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
WHAT YOU WILL
LEARN
• Problems with legacy code • How isolation helps • How to unit test legacy code (demo)
Legacy code is… Code without testsMichael Feathers
Developers write millions of lines of legacy code
every day!
Legacy Code Problems
• Code is tangled• Complex Tests• Death spiral• Catch-22
Isolate it… (The most effective way to test legacy code )
• Fake Dependencies
• Change Code Behavior
• Verify Calls
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
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
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
Cannot call constructor because of dependencies
Need method to return fake value
Nothing to assert
Fake an object to change its behavior.
Need to change behavior of a private method
Need to Fake an object that is created in the code
• Isolate Dependencies
• Control Behavior
• Assert Calls
Fake It Till You Make It!
PowerfulMocking
Visual Coverage SmartRunner
Typemock Isolator
Productivity*
* Complete Edition Only
• Trial License• Online webinars• Cookbook• Team Demonstration
You can get more resources
www.typemock.com
blog.typemock.com
@typemock
Questions?
Image creditsflickr@33909700@N02flickr@jamesukflickr@nikioflickr@duchampflickr@kalyan02flickr@clarkstonscampflicker@95786359@N05