36
Test-Driven Sitecore

Test-Driven Sitecore

  • Upload
    velir

  • View
    44

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Test-Driven Sitecore

Test-Driven Sitecore

Page 2: Test-Driven Sitecore

@DanSolovaySitecore Practice Lead at VelirMVP x 3

dansolovay.comvelir.com/blog

Page 3: Test-Driven Sitecore

What’s the plan?1. Unit Testing and Test-Driven Development2. Isolating dependencies3. Isolating Sitecore4. Testing Sitecore itself5. How to get started

Page 4: Test-Driven Sitecore

1. Unit Testing and Test-Driven Development

Page 5: Test-Driven Sitecore

What is a Unit Test?

• Can be automated• In memory

(no DB or File access)• Consistent & Fast• Single Logical Concept

Page 6: Test-Driven Sitecore

Unit Testing is a Waste of Time

…unless you plan to make changes to your code.

Page 7: Test-Driven Sitecore

Unit Tests Let You…• Change code with confidence• Refactor• Keep code simple• Work well with others

Page 8: Test-Driven Sitecore

Test-Driven Development

• RED: Make a failing test.• GREEN: Make it pass.• REFACTOR: Make it clean.

Page 9: Test-Driven Sitecore

This is stupid!"It's going to slow me down, it's a waste of time and effort, it will just break my flow." 

Page 10: Test-Driven Sitecore

However…Imagine if you walked into a room full of people working this way. A minute ago, all of their code worked.

Robert Martin, The Three Rules of TDDbit.ly/tdd3rules

Page 11: Test-Driven Sitecore

Calculator KataA classic exercise to learn TDD bit.ly/tdd-kata

Page 12: Test-Driven Sitecore
Page 13: Test-Driven Sitecore

Recap• Code is always working• RED: Add functionality• GREEN: Add design• Practice, practice, practice

Page 14: Test-Driven Sitecore

2. Isolating Dependencies

Page 15: Test-Driven Sitecore

IsolationHow do I keep out the real world?• Talk to interfaces• Wire with AutoFac• Fake with NSubstitute

Page 16: Test-Driven Sitecore

AutoFac• Constructor injection• Interfaces Classes• Code to abstractions

Page 17: Test-Driven Sitecore

NSubstitute• Create fakes from interfaces• Script output• Verify input

Page 18: Test-Driven Sitecore
Page 19: Test-Driven Sitecore

Recap• MVC + AutoFac:

Interfaces Production Objects• NSubstitute:

Interfaces Fakes

Page 20: Test-Driven Sitecore

3. Isolating Sitecore

Page 21: Test-Driven Sitecore

Sitecore and TestabilitySitecore items are FAT objects

• Knowledge of database• Hard to “new up”• God-like Static methods

Page 22: Test-Driven Sitecore

Glass Mapper• Injects items into light objects• Plays well with AutoFac & MVC• Code Gen option

Documentation: http://glass.lu

Page 23: Test-Driven Sitecore
Page 24: Test-Driven Sitecore

Sitecore.FakeDB• Creates fake Items• Can fake:

• Hierarchies• Templates• Security

• Still in Development

Github: bit.ly/FakeDB

Page 25: Test-Driven Sitecore
Page 26: Test-Driven Sitecore

Recap• Glass for isolating Sitecore• Sitecore.FakeDB for legacy code

Page 27: Test-Driven Sitecore

4. Testing Sitecore Itself

Page 28: Test-Driven Sitecore

Codeflood Test Runner• Available at

CodeFlood.net• Useful for testing

Sitecore behavior

Page 29: Test-Driven Sitecore
Page 30: Test-Driven Sitecore

Recap• Integration tests talk to real things• Use for testing Sitecore tools• Use unit tests to build logic

Page 31: Test-Driven Sitecore

5. How to Get Started

Page 32: Test-Driven Sitecore

Some Practical Tips• Start small. One hour a week.• Pair up.• Keep tests clean.• Keep tests passing. • Do a Kata workshop. • Do TDD Ping Pong.

Page 33: Test-Driven Sitecore

NSubstituteReSharperNCrunchXUnit?

Page 34: Test-Driven Sitecore

To Learn More• My talk videos: bit.ly/tdd-videos• String Calculator: bit.ly/tdd-kata• Alistair Deneys’ blog: bit.ly/sitecore-testing

Page 35: Test-Driven Sitecore

Photo CreditsClock:

© littlenelly, FlickrTraffic Light:

© Niels Sienaert, FlickrLighthouse:

© Dennis Jarvis, FlickrScrewdriver:

© Gal, FlickrDan:

© Chris Brady, Velir

Page 36: Test-Driven Sitecore

Thanks…

@DanSolovay@Velir