Upload
typemock
View
782
Download
3
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
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