mspec talk
DESCRIPTION
IntrodTRANSCRIPT
![Page 1: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/1.jpg)
INTRODUCTIONTO MSPEC Edward Wilde
AND OTHER STUFF
![Page 2: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/2.jpg)
A LITTLEABOUT ME
I’M NOT AN EXPERTI’M NOT RELIGIOUS
BUT SOME CLIENTS ARE
✘
![Page 3: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/3.jpg)
UNIT TESTINGBASICS
ArrangeAct Assert
"Arrange-Act-Assert"a pattern for arranging and formatting code in UnitTest methods:http://c2.com/cgi/wiki?ArrangeActAssert
Arrange all necessary preconditions and inputs.
Act on the object or method under test.
Assert that the expected results have occurred.
![Page 4: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/4.jpg)
CODEBREAK
DEMO ARRANGE ACT ASSERT
![Page 5: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/5.jpg)
BDDWHAT IS IT?
TDD with some structure
• Test names should be sentences
• Tests should be focused
• Tracing requirements, executable acceptance criteria
![Page 6: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/6.jpg)
STORY BASEDTESTING
Feature: Short & clearAs an actor of the systemI want to perform some actionIn order to realize some business value
Scenario: Some business situationGiven some contextAnd some other conditionWhen the actor perform an actionThen the outcome is achievedAnd some other outcome is achieved
Scenario: Some other situation related to the same story
User story
AcceptanceCriteria
![Page 7: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/7.jpg)
USER STORY EXAMPLE
Feature: Account transferAs a customerI want to transfer money between accounts online
In order to avoid having to telephone my bank to carry do this
Scenario: Account has required fundsGiven the source account is sufficientAnd the customer has signed up to online bankingWhen the customer transfers money between accountsThen the target account is credited immediatelyAnd the source account is debited immediately
Scenario: Account has insufficient funds Given the source account is insufficient funds...
![Page 8: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/8.jpg)
CODEBREAK
Creating a BDD test using SPEC FLOW
![Page 9: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/9.jpg)
ENTERMSPEC
CONTEXT SPECIFICATION FRAMEWORK
CONTEXT == ARRANGE or GIVEN
SPECIFICATION == ASSERT or THEN
DEVELOPER CENTRIC
BETTER FOR UNIT TESTING
![Page 10: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/10.jpg)
TESTSTRUCTURE
when_describe_context { Establish context => ()
Because of = () =>...
It should_...() =>...
It should_...() =>...
Cleanup test_data () =>...}
Arrange
Act
Assert
Given
When
Then
![Page 11: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/11.jpg)
INSTALLATION
Using chocolatey:cinst machine.specificationsrun InstallResharperRunner.7.1
Resharper live templates
Resharper settings – see resources
![Page 12: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/12.jpg)
CODEBREAK
Creating an mspec test
![Page 13: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/13.jpg)
TESTINGBEHAVIOR
Behaves_like<TBehavior>
[Behaviors]public class AccountInCredit{ protected static Account account; It should_have_a_positive_balance = () => account.Balance.ShouldBeGreaterThan(0m);}
![Page 14: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/14.jpg)
CODEBREAK
Creating an mspec test using behaviors
![Page 15: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/15.jpg)
MACHINEFAKES
Integrates machine.specifications and mocking
Auto-mocking container
Choose between:RhinoMocks, Moq, NSubstitue and
FakeItEasy
Install-Package machine.specifications.moq
![Page 16: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/16.jpg)
MACHINE FAKESWITHFAKES
WithFakes
Base class giving you access to
An<TFake> or Some<TFake> to create fakes
WhenToldTo(x=>x.foo).Return(3) // create an expectationWasToldTo(x=>x.foo) // verfiy an expectationParam<T>
![Page 17: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/17.jpg)
CODEBREAK
Creating a test using WithFakes
![Page 18: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/18.jpg)
MACHINE FAKESWITHSUBJECT
WithSubject<T>
Automocking containerCreates subject under test for you
The<Tinterface> access existing fakes
![Page 19: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/19.jpg)
CODEBREAK
Creating a test using WithSubject<T>
![Page 20: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/20.jpg)
MACHINE.FAKESREUSING CONTEXT
OnEstablish context = fake =>{ fake
.The<IFoo>
.WhenToldTo(x=>x.Bar())
.Return(“Baz”); }
OnCleanup sub
![Page 21: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/21.jpg)
CODEBREAK
Creating a test reusing context setup code.
![Page 22: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/22.jpg)
MSPECALT
SHARPDEVELOP
TEAM CITY
COMMAND LINE
MSBUILD TASK
![Page 23: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/23.jpg)
LESSONSI LEARNED
Treat test code like production code
Use DI and Factories
Be wary of inheritance
Tests should be simple
Don’t be scared to delete tests
![Page 24: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/24.jpg)
RESOURCES
Machine.Specificationshttps://github.com/machine/machine.specifications
Machine.Fakeshttps://github.com/machine/machine.fakes
Resharper templateshttp://therightstuff.de/2010/03/03/MachineSpecifications-Templates-For-ReSharper.aspx
Resharper settingshttp://www.thebooleanfrog.com/post/2011/11/24/ReSharper-StyleCop-and-MSpec-All-Together-Now.aspx
![Page 25: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/25.jpg)
PROJECTS USINGMSPEC
Fluent NHibernatehttp://www.fluentnhibernate.org/
Should Assertion Libraryhttp://should.codeplex.com
EventStorehttps://github.com/joliver/EventStore
Who-Can-Help-Mehttps://github.com/jongeorge1/Who-Can-Help-Me
![Page 26: Mspec talk](https://reader034.vdocument.in/reader034/viewer/2022051513/5462d0a1b4af9f491c8b4883/html5/thumbnails/26.jpg)
DRIVEHOME SAFELY
blogs.edwardwilde.com