Top Banner
.NEXT PRESENTS MOSCOW, RUSSIA MON. 8 th DEC. 2014 MOCKS, STUBS and ROCK’N’ROLL maintainable unit tests with AutoFixture Vladimir Almaev
17
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

.NEXT PRESENTSMOSCOW, RUSSIA MON. 8thDEC. 2014

MOCKS, STUBSand

ROCK’N’ROLLmaintainable unit tests

with AutoFixture

Vladimir Almaev

Page 2: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

Goals

•Dig into common maintainability problems

•Discover solutions• Minimum amount of code

• Keep only essential parts of the tests

• Follow DRY

Page 3: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

Arrange

Page 4: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

Act

Page 5: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

Assert

Page 6: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

Teardown

Page 7: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

[Fact]public void NirvanaGig_Always_ShouldBeAwesome() {

}

Pain Points• Constructors

• hard to maintain

• Stubs, Mocks, Fakes

• boring code

• Hard-coded literals

• are they essentials?

• Unnecessary details

• too fat Arrange

phase

var stage = new Stage(new Lights(10),new SoundSystem(),new Guitar("Fender Dzhigurda", 6),new BassGuitar("Gibson Ripper", 4),new Drums("Tama Granstar II"));

var performer = new Band("Nirvana",new Musician("Kurt Cobain"),new Musician("Khrist Novoselic"),new Musician("Dave Grohl"));

var crowd = new Crowd(10000);var sut = new Gig(stage, performer, crowd);

// Arrange

sut.Perform(); // Act

Assert.Equal(Mood.Awesome, crowd.Mood); // Assert

Page 8: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

[Fact]public void NirvanaGig_Always_ShouldBeAwesome() {

}

Pain Points• Constructors

• hard to maintain

• Stubs, Mocks, Fakes

• boring code

• Hard-coded literals

• are they essentials?

• Unnecessary details

• too fat Arrange

phase

var stage = new Stage(new Lights(10),new SoundSystem(),new Guitar("Fender Dzhigurda", 6),new BassGuitar("Gibson Ripper", 4),new Drums("Tama Granstar II"));

var performer = new Band("Nirvana",new Musician("Kurt Cobain"),new Musician("Khrist Novoselic"),new Musician("Dave Grohl"));

var crowd = new Crowd(10000);var sut = new Gig(stage, performer, crowd);

// Arrange

sut.Perform(); // Act

Assert.Equal(Mood.Awesome, crowd.Mood); // Assert

Page 9: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

sut.Perform();

Assert.Equal(Mood.Awesome, crowd.Mood);

performer.Name = "Nirvana";

Page 10: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

[

{]

Page 11: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

[

{]

Page 12: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

Show me the code!

Page 13: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

Demo recap

• Imperative tests by manual creating Fixture

•Declarative tests• AutoData, InlineAutoData, Custom domain attributes• xUnit.net, NUnit

•Auto-mocking container• Moq, Foq, NSubstitute, FakeItEasy, RhinoMocks

•Repetitive tests using AutoFixture.Idioms• GuardClauseAssertion

Page 14: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

/valmaev/mocks-stubs-rocknroll

Page 15: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

Further reading

•Mark Seemann’s Pluralsight courses: • Outside-In TDD

• Advanced Unit Testing

•Mark Seemann’s blog: http://blog.ploeh.dk

•Nikos Baxevanis blog: http://nikosbaxevanis.com

Page 16: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

About me

@vladimir_almaev

valmaev

http://valmaev.github.io

Page 17: Mocks, Stubs and Rock'n'roll: maintainable unit tests with AutoFixture

Thanks