Code Contracts and Pex : Power Charge your Assertions and Unit Tests
description
Transcript of Code Contracts and Pex : Power Charge your Assertions and Unit Tests
![Page 1: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/1.jpg)
Code Contracts and Pex:Power Charge your Assertions and Unit TestsMike Barnett, Nikolai TillmannPrincipal Research Software Design EngineersMicrosoft Research
VTL01
![Page 2: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/2.jpg)
What you will learn Code Contracts
> Method contracts> Contract.Requires> Contract.Ensures
> Object invariants> Contract.Invariant
> Interface contracts> Tools
> Runtime Checking> Static Checking> Documentation
Pex
> Automated Test Generation
> Parameterized Unit Testing
> Stubs and Moles>Lightweight mocking>For sealed classes,
static methods, interfaces
Expressing design intent
Advanced unit testing
![Page 3: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/3.jpg)
Objective: Implement WebService.Process
class WebService { WebService(IWarehouse warehouse) { … } void Process(Order order) { … }}class Order { string UserName { get; … } int ProductID { get; … } int Quantity { get; … }}interface IWarehouse { Item RemoveFromInventory(int productID); void ShipToCustomer(string userName, int itemID);}class Item { … }
![Page 4: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/4.jpg)
Introduction toCode Contracts, Pex
demo
![Page 5: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/5.jpg)
Code Contracts – The Basicspublic WebService(IWarehouse store) {
}this.store = store;
![Page 6: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/6.jpg)
Code Contracts – The Basics
> Requires: What must be true at method entry
public WebService(IWarehouse store) {
}this.store = store;
Contract.Requires(store != null);
![Page 7: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/7.jpg)
Code Contracts – The Basics
> Requires: What must be true at method entry
> Ensures: What must be true at method exit> One source: Many uses
> Runtime Checking> Static Checking> Documentation
public WebService(IWarehouse store) {
}
Contract.Requires(store != null);Contract.Ensures(this.store != null);this.store = store;
![Page 8: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/8.jpg)
Code Contracts – The Not So Basics
> Invariants: internal object consistency> Conditions over (private and protected) fields> What must be true at public method exits> Can have multiple invariant methods
[ContractInvariantMethod]void ObjectInvariant() { Contract.Invariant(this.store != null);}
![Page 9: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/9.jpg)
Code Contracts – Interface Contracts> Contracts for those hard-to-reach
areas…> Works for abstract classes too
public interface IWarehouse { …}
[ContractClass(typeof(IWarehouseContract))]public interface IWarehouse { …}[ContractClassFor(typeof(IWarehouse))]public class IWarehouseContract : IWarehouse { Item IWarehouse.RemoveFromInventory(int productID) { Contract.Ensures(Contract.Result<Item>() != null); … } …}
![Page 10: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/10.jpg)
Code Contracts – Interface Contracts> Contracts for those hard-to-reach
areas…> Works for abstract classes too
public interface IWarehouse { …}
[ContractClass(typeof(IWarehouseContract))]public interface IWarehouse { …}[ContractClassFor(typeof(IWarehouse))]public class IWarehouseContract : IWarehouse { Item IWarehouse.RemoveFromInventory(int productID) { Contract.Ensures(Contract.Result<Item>() != null); … } …}
linked via attributes
![Page 11: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/11.jpg)
Code Contracts – Interface Contracts> Contracts for those hard-to-reach
areas…> Works for abstract classes too
public interface IWarehouse { …}
[ContractClass(typeof(IWarehouseContract))]public interface IWarehouse { …}[ContractClassFor(typeof(IWarehouse))]public class IWarehouseContract : IWarehouse { Item IWarehouse.RemoveFromInventory(int productID) { Contract.Ensures(Contract.Result<Item>() != null); … } …}
![Page 12: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/12.jpg)
Controlling the Contracts
![Page 13: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/13.jpg)
WebService.cs
WebService.dll
Runtime Checking — Test Build
IL from body
IL from requires
IL from ensures
csc/vbc/… +ccrewrite
public WebService(IWarehouse store) {
}
this.store = store;
Contract.Requires(store != null);Contract.Ensures(this.store != null);
![Page 14: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/14.jpg)
WebService.cs
WebService.dll
Runtime Checking — Release Build
IL from body
IL from requires
> For libraries with general clients
csc/vbc/… +ccrewrite
public WebService(IWarehouse store) {
}
this.store = store;
Contract.Requires(store != null);Contract.Ensures(this.store != null);
![Page 15: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/15.jpg)
WebService.cs
WebService.dll
Runtime Checking — Release Build
IL from bodycsc/vbc/…
public WebService(IWarehouse store) {
}
this.store = store;
Contract.Requires(store != null);Contract.Ensures(this.store != null);
> For trusted clients
![Page 16: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/16.jpg)
WebService.xml
WebService.Contracts.dll
Documentation
IL from requires
IL from ensures
> XML used by Sandcastle to generate MSDN style docs
<member name="M:PDC.WebService.#ctor(PDC.IWarehouse)"><summary>Constructs a new instance for processing orders against the specified warehouse.</summary><param name="store">The warehouse this instance is to use. </param></member> WebService.x
ml<member name="M:PDC.WebService.#ctor(PDC.IWarehouse)"><summary>Constructs a new instance for processing orders against the specified warehouse.</summary><param name="store">The warehouse this instance is to use. </param><requires> store != null </requires><ensures> this.store != null </ensures></member>
ccdocgen
![Page 17: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/17.jpg)
Pex − Parameterized Unit TestingBeyond Unit Testing> Generated stub:
[PexMethod]public void Process(WebService target, Order order) { target.Process(order); // TODO: Add assertions here}
![Page 18: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/18.jpg)
Pex − Parameterized Unit TestingBeyond Unit Testing> Generated stub:
[PexMethod]public void Process(WebService target, Order order) { target.Process(order); // TODO: Add assertions here}
Attribute marks
Parameterized Unit Test
![Page 19: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/19.jpg)
Pex − Parameterized Unit TestingBeyond Unit Testing> Idea:
Turn values that shouldn’t matter into parameters
> Pex chooses relevant values> Pex executes and analyzes code> Constraint solver determines values
that trigger code paths> Result: Traditional unit tests
[PexMethod]public void Process(WebService target, Order order) { target.Process(order); // TODO: Add assertions here}
![Page 20: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/20.jpg)
[PexMethod]public void Process(WebService target, Order order){ target.Process(order);}
Pex − Choosing Relevant Values
![Page 21: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/21.jpg)
[PexMethod]public void Process(WebService target, Order order){ target.Process(order);}
Pex − Choosing Relevant ValuesPex executes and monitors
test
![Page 22: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/22.jpg)
[PexMethod]public void Process(WebService target, Order order){ target.Process(order);}
Pex − Choosing Relevant Values
Pex will generate “relevant” values
(null, new X(), …)
![Page 23: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/23.jpg)
[PexMethod]public void Process( [PexAssumeUnderTest] WebService target, Order order){ target.Process(order);}
Pex − Choosing Relevant Values
Another attribute to mark instance-under-
test, may not be null.
![Page 24: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/24.jpg)
Pex − Choosing Relevant Values[PexMethod]public void Process( [PexAssumeUnderTest] WebService target, Order order){ target.Process(order);}
public void Process(Order order) { if (order == null) return; for (int i = 0; i < order.Quantity; i++) { var item = store.RemoveFromInventory(order.ProductID); store.ShipToCustomer(order.UserName, item.ItemID); }}
Pex follows calls
![Page 25: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/25.jpg)
Pex − Choosing Relevant Values
public void Process(Order order) { if (order == null) return; for (int i = 0; i < order.Quantity; i++) { var item = store.RemoveFromInventory(order.ProductID); store.ShipToCustomer(order.UserName, item.ItemID); }}
[PexMethod]public void Process( [PexAssumeUnderTest] WebService target, Order order){ target.Process(order);}
Pex discoversnon-null constraint
→ test case!
![Page 26: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/26.jpg)
public void Process(Order order) { if (order == null) return; for (int i = 0; i < order.Quantity; i++) { var item = store.RemoveFromInventory(order.ProductID); store.ShipToCustomer(order.UserName, item.ItemID); }}
[PexMethod]public void Process( [PexAssumeUnderTest] WebService target, Order order){ target.Process(order);}
Pex − Choosing Relevant Values
Pex detects loop→ test cases for 0, 1, 2
iterations
![Page 27: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/27.jpg)
public void Process(Order order) { if (order == null) return; for (int i = 0; i < order.Quantity; i++) { var item = store.RemoveFromInventory(order.ProductID); store.ShipToCustomer(order.UserName, item.ItemID); }}
[PexMethod]public void Process( [PexAssumeUnderTest] WebService target, Order order){ target.Process(order);}
Pex − Choosing Relevant Values
Pex detects possiblenull dereference
→ test case!
![Page 28: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/28.jpg)
public void Process(Order order) { if (order == null) return; for (int i = 0; i < order.Quantity; i++) { var item = store.RemoveFromInventory(order.ProductID); store.ShipToCustomer(order.UserName, item.ItemID); }}
[PexMethod]public void Process( [PexAssumeUnderTest] WebService target, Order order){ target.Process(order);}
Pex − Choosing Relevant Values
Pex detects possiblenull dereference
→ test case!
[TestMethod]public void Process03() { var webService = new WebService((IWarehouse)null); var order = new Order((string)null, 0, 1); this.Process(webService, order);}
![Page 29: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/29.jpg)
public void Process(Order order) { if (order == null) return; for (int i = 0; i < order.Quantity; i++) { var item = store.RemoveFromInventory(order.ProductID); store.ShipToCustomer(order.UserName, item.ItemID); }}
[PexMethod]public void Process( [PexAssumeUnderTest] WebService target, Order order){ target.Process(order);}
Pex − Choosing Relevant Values
![Page 30: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/30.jpg)
IWarehouse store;public void Process(Order order) { if (order == null) return; for (int i = 0; i < order.Quantity; i++) { var item = store.RemoveFromInventory(order.ProductID); store.ShipToCustomer(order.UserName, item.ItemID); }}
interface IWarehouse { … }
Wait a moment…> IWarehouse is an interface.> How to test code that depends on
interfaces?
![Page 31: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/31.jpg)
Pex − Stubs for Interfaces> Generated class for every interface
interface IWarehouse { // hand-written void ShipToCustomer(string userName, int itemID); …}
class SIWarehouse : IWarehouse { // generated Action<string, int> ShipToCustomerStringInt32 { get; set; } void IWarehouse.ShipToCustomer(string userName, int itemID) { return ShipToCustomerStringInt32(userName, itemID); } …}
![Page 32: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/32.jpg)
Pex − Stubs for Interfaces> Generated class for every interface
> Delegate-property for every method
interface IWarehouse { // hand-written void ShipToCustomer(string userName, int itemID); …}
class SIWarehouse : IWarehouse { // generated Action<string, int> ShipToCustomerStringInt32 { get; set; } void IWarehouse.ShipToCustomer(string userName, int itemID) { return ShipToCustomerStringInt32(userName, itemID); } …}
![Page 33: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/33.jpg)
Pex − Stubs for Interfaces> Generated class for every interface
> Delegate-property for every method
interface IWarehouse { // hand-written void ShipToCustomer(string userName, int itemID); …}
class SIWarehouse : IWarehouse { // generated Action<string, int> ShipToCustomerStringInt32 { get; set; } void IWarehouse.ShipToCustomer(string userName, int itemID) { return ShipToCustomerStringInt32(userName, itemID); } …}
Name mangling to distinguish overloads
![Page 34: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/34.jpg)
Pex − Stubs for Interfaces> Generated class for every interface
> Delegate-property for every method
interface IWarehouse { // hand-written void ShipToCustomer(string userName, int itemID); …}
class SIWarehouse : IWarehouse { // generated Action<string, int> ShipToCustomerStringInt32 { get; set; } void IWarehouse.ShipToCustomer(string userName, int itemID) { return ShipToCustomerStringInt32(userName, itemID); } …}
![Page 35: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/35.jpg)
Pex − Stubs for Interfaces> Generated class for every interface
> Delegate-property for every method> Implementation calls delegates
interface IWarehouse { // hand-written void ShipToCustomer(string userName, int itemID); …}
class SIWarehouse : IWarehouse { // generated Action<string, int> ShipToCustomerStringInt32 { get; set; } void IWarehouse.ShipToCustomer(string userName, int itemID) { return ShipToCustomerStringInt32(userName, itemID); } …}
![Page 36: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/36.jpg)
Pex − Stubs for Interfaces> Generated class for every interface
> Delegate-property for every method> Implementation calls delegates
interface IWarehouse { // hand-written void ShipToCustomer(string userName, int itemID); …}
class SIWarehouse : IWarehouse { // generated Action<string, int> ShipToCustomerStringInt32 { get; set; } void IWarehouse.ShipToCustomer(string userName, int itemID) { return ShipToCustomerStringInt32(userName, itemID); } …}
![Page 37: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/37.jpg)
Pex − Parameterized Unit TestsPutting it all together[PexMethod]public void ProcessOrderAndCheckQuantity(Order order) {
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (name, id) => count++ };
var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
![Page 38: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/38.jpg)
Pex − Parameterized Unit TestsPutting it all together[PexMethod]public void ProcessOrderAndCheckQuantity(Order order) {
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (name, id) => count++ };
var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
This test should work for any 'Order'
![Page 39: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/39.jpg)
Pex − Parameterized Unit TestsPutting it all together[PexMethod]public void ProcessOrderAndCheckQuantity(Order order) {
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (name, id) => count++ };
var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
Customized stub(here: to count invocations)
![Page 40: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/40.jpg)
Pex − Parameterized Unit TestsPutting it all together[PexMethod]public void ProcessOrderAndCheckQuantity(Order order) {
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (name, id) => count++ };
var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
delegate(string name, int id) { count++;}
![Page 41: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/41.jpg)
Pex − Parameterized Unit TestsPutting it all together[PexMethod]public void ProcessOrderAndCheckQuantity(Order order) {
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (name, id) => count++ };
var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
var warehouse = new SIWarehouse();warehouse.ShipToCustomerStringInt32 = ...;
![Page 42: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/42.jpg)
Pex − Parameterized Unit TestsPutting it all together[PexMethod]public void ProcessOrderAndCheckQuantity(Order order) {
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (name,id)=> count++ };
var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
Executing code under test
![Page 43: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/43.jpg)
Pex − Parameterized Unit TestsPutting it all together[PexMethod]public void ProcessOrderAndCheckQuantity(Order order) {
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (name, id) => count++ };
var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
Asserting expected results
![Page 44: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/44.jpg)
Pex − Parameterized Unit TestsPutting it all together[PexMethod]public void ProcessOrderAndCheckQuantity(Order order) { Contract.Assume(order != null);
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (name, id) => count++ };
var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
Assumptions inparameterized unit test
![Page 45: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/45.jpg)
Parameterized Unit Testing
demo
![Page 46: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/46.jpg)
Testability> Code becomes hard to test if it
> interacts with the environments> depends on sealed classes, static
methods, classes with non-public constructors, …
public void Process(Order order) { if (order == null) return; for (int i = 0; i < order.Quantity; i++) { var item = store.RemoveFromInventory(order.ProductID); if (DateTime.Now != new DateTime(2000, 1, 1)) store.ShipToCustomer(order.UserName, item.ItemID); }}
![Page 47: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/47.jpg)
Moles – Detouring of .NET methods> What if we could replace any .NET
method with a delegate?
DateTime.Now = () => new DateTime(2000, 1, 1);
![Page 48: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/48.jpg)
Moles – Detouring of .NET methods> What if we could replace any .NET
method with a delegate?
> Possible with Moles
DateTime.Now = () => new DateTime(2000, 1, 1);
MDateTime.NowGet = () => new DateTime(2000, 1, 1);
![Page 49: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/49.jpg)
Pex − Moles to Detour Untestable Code> “Mole classes” can be generated
for every existing class
struct DateTime { static DateTime Now { get; } …}
class MDateTime { // generated static Func<DateTime> NowGet { set { /*magic*/ } } …}
![Page 50: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/50.jpg)
Pex − Moles to Detour Untestable Code> “Mole classes” can be generated
for every existing class> Property of delegate type for every
methodstruct DateTime { static DateTime Now { get; } …}
class MDateTime { // generated static Func<DateTime> NowGet { set { /*magic*/ } } …}
![Page 51: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/51.jpg)
Pex − Moles to Detour Untestable Code> “Mole classes” can be generated
for every existing class> Property of delegate type for every
methodstruct DateTime { static DateTime Now { get; } …}
class MDateTime { // generated static Func<DateTime> NowGet { set { /*magic*/ } } …}
After attaching delegate, all
future calls get detoured to
attached delegate handler (realized by code instrumentation)
![Page 52: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/52.jpg)
Pex − Parameterized Unit TestsPutting it all together: Pex and Stubs and Moles[PexMethod]public void ProcessOrderAndCheckQuantityMoles(Order order, DateTime now) { Contract.Assume(order != null);
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (userName, id) => count++ };
MDateTime.NowGet = () => now; var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
![Page 53: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/53.jpg)
Pex − Parameterized Unit TestsPutting it all together: Pex and Stubs and Moles[PexMethod]public void ProcessOrderAndCheckQuantityMoles(Order order, DateTime now) { Contract.Assume(order != null);
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (userName, id) => count++ };
MDateTime.NowGet = () => now; var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
![Page 54: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/54.jpg)
[PexMethod]public void ProcessOrderAndCheckQuantityMoles(Order order, DateTime now) { Contract.Assume(order != null);
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (userName, id) => count++ };
MDateTime.NowGet = () => now; var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
Pex − Parameterized Unit TestsPutting it all together: Pex and Stubs and Moles
We detour all calls to DateTime.Now to
delegate
![Page 55: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/55.jpg)
[PexMethod]public void ProcessOrderAndCheckQuantityMoles(Order order, DateTime now) { Contract.Assume(order != null);
int count = 0; var warehouse = new SIWarehouse() { ShipToCustomerStringInt32 = (userName, id) => count++ };
MDateTime.NowGet = () => now; var target = new WebService(warehouse); target.Process(order);
Contract.Assert(order.Quantity == count);}
Pex − Parameterized Unit TestsPutting it all together: Pex and Stubs and Moles
Result of DateTime.Now becomes test parameter.
![Page 56: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/56.jpg)
Molesto Detour Untestable Code
demo
![Page 57: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/57.jpg)
Code Contracts − SummaryYou have the library, get the tools now!> Contracts Library in .NET version 4> DevLabs today:
> Coming soon:> VS 2010 Adornments
> Pervasive contracts: use them everywhere!
> Runtime Checker> Static Checker> Documentation
Generator
> Visual Studio Addin> Msbuild Integration> (pre-v4 contract
library)
![Page 58: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/58.jpg)
Pex − SummaryGet it now!> Parameterized Unit Testing
> Write more expressive unit tests> Automated test generation
> Pex generates traditional unit tests> Result: small test suite, high coverage
> Stubs and Moles> Lightweight Mocking> For interfaces, sealed classes and static
methods> Replace any .NET method with your
delegate
![Page 59: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/59.jpg)
http://msdn.microsoft.com/devlabs/> Download, visit forums> Give your feedback! Shape the
future.
![Page 60: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/60.jpg)
Come See Us!> Code Contracts
and Pex>Big
RoomMP-BP03
![Page 61: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/61.jpg)
YOUR FEEDBACK IS IMPORTANT TO US! Please fill out session evaluation
forms online atMicrosoftPDC.com
![Page 62: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/62.jpg)
Learn More On Channel 9> Expand your PDC experience through
Channel 9
> Explore videos, hands-on labs, sample code and demos through the new Channel 9 training courses
channel9.msdn.com/learnBuilt by Developers for Developers….
![Page 63: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/63.jpg)
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
![Page 64: Code Contracts and Pex : Power Charge your Assertions and Unit Tests](https://reader036.fdocuments.in/reader036/viewer/2022062315/56816468550346895dd6534a/html5/thumbnails/64.jpg)