Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Table-Driven Testing

Table-Driven Testing

Nicolas Mérouze

June 03, 2014
Tweet

More Decks by Nicolas Mérouze

Other Decks in Programming

Transcript

  1. Scenario Outline: eating! Given there are <start> cucumbers! When I

    eat <eat> cucumbers! Then I should have <left> cucumbers! ! Examples:! | start | eat | left |! | 12 | 5 | 7 |! | 20 | 5 | 15 |!
  2. Validations • Utilisation de matchers : it { should validate_presence_of(:title)

    } • Tests individuels pour chaque validation • Tests avec des factories
  3. it "should have valid factory" do! FactoryGirl.build(:user).should be_valid! end! !

    it "should require a name" do! FactoryGirl.build(:user, name: "").should_not be_valid! end
  4. describe User do! describe ".create" do! it "persists a user

    with valid attributes" do! user = User.create(name: "Jane")! ! expect(user).to be_persisted! end! ! it "does not persist if attributes are not valid" do! user = User.create! ! expect(user).not_to be_persisted! expect(user.errors[:name]).to include("can't be blank")! end! end! end!
  5. describe User do! describe ".create" do! def invalid_examples! [! {

    attributes: FactoryGirl.attributes_for(:user, name: ""), errors: { name: ["can't be blank"] } },! { attributes: FactoryGirl.attributes_for(:user, email: "janeATicloudDOTcom"), errors: { email: ["is invalid."] } }! ]! end! ! it "persists a user with valid attributes" do! user = User.create(FactoryGirl.attributes_for(:user))! ! expect(user).to be_persisted! end! ! it "does not persist if attributes are not valid" do! invalid_examples.each do |example|! user = User.create(example[:attributes])! ! expect(user).not_to be_persisted! expect(user.errors).to eq(example[:errors])! end! end! end! end!
  6. Conclusion • Clarté • Maintenabilité • Force à faire des

    méthodes simples… • …et des vrais tests • Utilisable dans n’importe quel langage