20

Click here to load reader

It's all about behaviour, also in php - phpspec

Embed Size (px)

DESCRIPTION

BDD is not only for rubists :)

Citation preview

Page 1: It's all about behaviour, also in php - phpspec

@liuggio

IT’S ALL ABOUT BEHAVIOUR-ALSO IN PHP-

PUG-ROMA 26-02-2013

Page 2: It's all about behaviour, also in php - phpspec

WHY WE DO TESTS?

> refactor

> automation

> verification

> ??

Page 3: It's all about behaviour, also in php - phpspec

WHO LOVES TDD?

"Test-Driven Development is a developer practice that involves writing tests before writing the code being tested."

<<TDD is a design tool

used to deliver high-quality code >>

REFACTOR IS TIME CONSUMING

Page 4: It's all about behaviour, also in php - phpspec

RED GREEN REFACTOR

TDD: THE INFINTE CYCLE

Page 5: It's all about behaviour, also in php - phpspec

"Run it, watch it fail, write just enough code to get it to pass, review the design, and remove duplication" > "Run it, watch it fail, write just enough code to get it to pass, review the design, and remove duplication" > "Run it, watch it fail, write just enough code to get it to pass, review the design, and remove duplication" > "Run it, watch it fail, write just enough code to get it to pass, review the design, and remove duplication" > "Run it, watch it fail, write just enough code to get it to pass, review the design, and remove duplication" > "Run it, watch it fail, write just enough code to get it to pass, review the design, and remove duplication" > "Run it, watch it fail, write just enough code to get it to pass, review the design, and remove duplication" > "Run it, watch it fail, write just enough code to get it to pass, review the design, and remove duplication" > "Run it, watch it fail, write just enough code to get it to pass, review the design, and remove duplication" > "Run it, watch it fail, write just enough code to get it to pass, review the design, and remove duplication"

TECHNOLOGIC

TDD: THE INFINTE CYCLE

Page 6: It's all about behaviour, also in php - phpspec

If you have a blank project, you have to start...

Where to start?

What to test?

What not to test?

How much tests?

Where to start?

TDD: mmm

Page 7: It's all about behaviour, also in php - phpspec

“If you need to test the insertion of an object into a collection and the collection is represented by an Array, with xUnit you should assert that the collection contains the object in the Array, but if the collection will change to another type of container, graph for example, the xUnit will fail, even if the behaviour is unchanged. “

TDD: mmmmmmmmm

Page 8: It's all about behaviour, also in php - phpspec

in BDD you are not testing

you are describing what that classes will do,

so you can continue creating the class

Specify the Behaviour by Examples

not testing a class.

BDD :)

Page 9: It's all about behaviour, also in php - phpspec

External behaviour - Story

Behat deals to have specifications that reflects the environment from the outside.Solve: Where to start, What to and not to test

Internal behaviour - Example

PHPSpec responds to the behaviour in the lower level, from the internal of the classes.

Solve all the TDD mmmmmmm

BDD :D

Page 10: It's all about behaviour, also in php - phpspec

1 start from Scenario

2 write a story that fail

go to phpspec2 internal level

3 write a fail Example

4 get it green

5 refactor

go to behat external level

7 refactor

BDD the double cycle

Page 11: It's all about behaviour, also in php - phpspec

"Spec BDD with phpspec

phpspec is a development tool, designed to help you achieve clean and working PHP code by using a technique derived from test-first development called (spec) behaviour driven developement, or SpecBDD."

Page 12: It's all about behaviour, also in php - phpspec

1. Installation composer

2. Code the behaviour: WAIT

we have to talk the same language

{ "require-dev": { "phpspec/phpspec2": "*" }, "config": { "bin-dir": "bin" }, "autoload": {"psr-0": {"": "src"}}, "minimum-stability": "dev"}

LET’S CODE

Page 13: It's all about behaviour, also in php - phpspec

In BDD you are going to 'Describe' the behaviour of a Class creating a `Spec` that will contain more Examples.

'example' is the class to write, before write the subject code.

'expectation' the behaviour that the subject code should have.

• Assertion becomes expectation.

• Test method becomes code example

• Test case becomes example group/Specification

DEFINITION

Page 14: It's all about behaviour, also in php - phpspec

Not really coding, command lining :)

Now Code the examples ...

$ bin/phpspec desc FakeTwitterSpecification for FakeTwitter created in spec/FakeTwitter.php.

$ bin/phpspec run -v -f prettify

CODING TIME

Page 15: It's all about behaviour, also in php - phpspec

BDD

Page 16: It's all about behaviour, also in php - phpspec

BDD you're doing it wrong

I Code, than I specify

Page 17: It's all about behaviour, also in php - phpspec

BDD you're doing it wrong

Cool I'm going to specify all my old

projects

Page 18: It's all about behaviour, also in php - phpspec

BDD you're doing it wrong

I don't care about documentation and

acceptance.

Page 19: It's all about behaviour, also in php - phpspec

1987 developer

I don't need unit tests, and I do not need to

specify

Page 20: It's all about behaviour, also in php - phpspec

REFERENCES

- http://www.slideshare.net/bmabey/the-why-behind-tddbdd-and-the-how-with-rspec

- The RSpec Book: Behaviour-Driven Development with RSpec, Cucumber, and Friends

- http://www.slideshare.net/Robbert/bdd-rspec

- phpspec.net

- behat.org