bending’autofixture’to’your’will’ · 2012-05-21 · goals’ autofixture’ provides...

23
Bending AutoFixture to your will Mark Seemann @ploeh h:p://www.flickr.com/photos/barnshaws/5757943464/

Upload: others

Post on 09-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

Bending  AutoFixture  to  your  will  

Mark  Seemann  @ploeh  

h:p://www.flickr.com/photos/barnshaws/5757943464/  

Page 2: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

Agenda  

Code  Code  Code  Code  Code  Code  Slides  

Page 3: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

Goals  

AutoFixture  provides  feedback  –  listen  to  it  

You  can  modify  

AutoFixture’s  behavior  to  fit  your  needs  

Favor  convenPons  over  specific  modificaPons  

Page 4: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

Test  Data  Builder  public  class  ContactBuilder  {          private  readonly  string  n;          private  readonly  string  p;            public  ContactBuilder()  :  this("Jane  Doe",  "55512345")  {  }            private  ContactBuilder(string  name,  string  phone)          {                  this.n  =  name;                  this.p  =  phone;          }            public  ContactBuilder  WithName(string  name)          {                  return  new  ContactBuilder(name,  this.p);          }            public  ContactBuilder  WithPhone(string  phone)          {                  return  new  ContactBuilder(this.n,  phone);          }            public  Contact  Build()          {                  return  new  Contact  {  Name  =  this.n,  Phone  =  this.p  };          }  }  

Page 5: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

ContactBuilder  examples  var  contact  =  new  ContactBuilder().Build();            var  contact  =  new  ContactBuilder().WithName("John  Doe").Build();            var  contact  =  new  ContactBuilder().WithPhone("12345678").Build();      

Page 6: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

Test  Data  Builder  

A  dream  to  use  

A  pain  to  maintain  

A  TDB  for  

every  SUT  

Page 7: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

AutoFixture  

Takes  away  the  pain  of  maintaining  TDBs   ReflecPon-­‐based   ConvenPon-­‐based  

Page 8: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

DEMO  Replace  ContactBuilder  with  AutoFixture  

Page 9: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

What  just  happened?  

The  concrete  ContactBuilder  was  replaced  

with  AutoFixture  

h:p://www.flickr.com/photos/omcoc/6751047205/  

Page 10: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

DEMO  Customize  the  Fixture  

Page 11: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

What  just  happened?  

The  phone  rule  was  

encapsulated  in  a  class  

This  provides  a  central  place  to  manage  the  rule  

h:p://www.flickr.com/photos/omcoc/6751047205/  

Page 12: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

DEMO  Move  towards  a  declaraPve  style  

Page 13: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

What  just  happened?  

Taking  advantage  of  xUnit.net’s  extensibility  model,  creaPon  of  object  instances  were  made  

declaraPve  

The  rest  of  the  talk  doesn’t  hinge  on  this  xUnit.net  feature  at  

It’s  just  a  cool  thing  to  do  

h:p://www.flickr.com/photos/omcoc/6751047205/  

Page 14: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

DEMO  Create  unique  phone  numbers  

Page 15: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

What  just  happened?  

Phone  numbers  were  made  unique  to  a  Fixture  

ParPal  delegaPon  to  Fixture’s  ability  to  create  unique  

numbers  

h:p://www.flickr.com/photos/omcoc/6751047205/  

Page 16: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

DEMO  Auto-­‐mocking  

Page 17: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

What  just  happened?  

Fixture  was  turned  into  an  Auto-­‐mocking  Container  

Moq  was  used  

Rhino  Mocks  also  supported  

OOB  

FakeItEasy  also  supported  OOB  

All  are  extensions  

h:p://www.flickr.com/photos/omcoc/6751047205/  

Page 18: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

DEMO  Circular  references  

Page 19: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

What  just  happened?  

Circular  references  are  a  design  smell  

However,  they  can  be  dealt  with  by  changing  the  

behavior  of  Fixture  

h:p://www.flickr.com/photos/omcoc/6751047205/  

Page 20: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

DEMO  ConvenPons  for  phone  numbers  

Page 21: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

What  just  happened?  

Manual  edit  using  the  Fluid  Builder  API  

ConvenPon  based  on  PropertyInfo  

Value  resolved  via  int  

Value  resolved  via  RangedNumberRequest  

h:p://www.flickr.com/photos/omcoc/6751047205/  

Page 22: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

To  go  

If  AutoFixture  can’t  create  an  instance  of  your  

API,  it’s  probably  your  

fault  

Use  AutoFixture’s  

many  extensibility  

points  to  make  it  behave  like  you’d  like  

Strive  towards  a  single  set  of  

convenPons  per  test  project  

Page 23: Bending’AutoFixture’to’your’will’ · 2012-05-21 · Goals’ AutoFixture’ provides feedback’– listen’to’it You’can’ modify AutoFixture’s’ behavior’to’

AutoFixture  • h:p://autofixture.codeplex.com/  • h:p://stackoverflow.com/quesPons/tagged/autofixture  • Nuget:  AutoFixture  

Mark  Seemann  • h:p://blog.ploeh.dk/  • @ploeh  

Nikos  Baxevanis  • h:p://www.nikosbaxevanis.com/bonus-­‐bits/  • @nikosbaxevanis  

Enrico  Campidoglio  • h:p://megakemp.wordpress.com/  • @ecampidoglio