Giter Site home page Giter Site logo

intro_to_testing's Introduction

intro_to_testing

Table of Contents

  1. Overview
  2. Learning Goals
  3. File Structure
  4. Setting up RSpec
  5. The Interaction
  6. Writing a Test
  7. Practice

Overview

Test Driven Development is a powerful tool used to drive your code. At Turing, TDD will be essential. RSpec is the testing framework that you will use during your time at Turing. This repository is used in conjunction with the intro_to_testing lesson and slides.

You will need to clone this repository to your local.

We will work with the files in this order:

  • interaction/example.md to reference the interaction pattern used in lesson.
  • spec/student_spec.rb to write tests first.
  • lib/student.rb to implement the code that the tests expect.

Learning Goals

  • Understand why we use tests
  • Understand the stages of a test
  • Understand an RSpec test
  • Use a variety of assertion methods

File Structure

This is how your file tree will be structured.

.
โ”œโ”€โ”€ lib
|   โ””โ”€โ”€ name_of_class.rb
โ””โ”€โ”€ spec
    โ””โ”€โ”€ name_of_class_spec.rb

The lib directory will hold all of your class files.

  • The lib files will be the name of the class. See above.

The spec directory will hold all of your test files.

  • The test files will be the name of the class followed by the word spec. See above.

To create a lib directory: (from your terminal)

mkdir lib

To create a spec directory: (from your terminal)

mkdir spec

Setting up RSpec

This is how you will install the gem:

gem install rspec

You will then use the following line of code at the top of each of your spec files:

require 'rspec'

The Interaction

In Mod 1 you will learn how to read an interaction pattern. The interaction pattern is a guide for what tests you will be writing and what the expected return value should be. It is in your best interest to follow the interaction pattern as it is given to you and pay attention to the details.

So here is an example of what that might look like.

Example:

pry(main)> require './lib/student'
=> true

pry(main)> student = Student.new('Penelope')
=> #<Student:0x007fa71e12c1f0 @cookies=[], @name="Penelope">

pry(main)> student.name
=> "Penelope"

pry(main)> student.cookies
=> []

pry(main)> student.add_cookie('Chocolate Chunk')
pry(main)> student.add_cookie('Snickerdoodle')

pry(main)> student.cookies
=> ["Chocolate Chunk", "Snickerdoodle"]

So what is happening? Let's look at the code below:

pry(main)> require './lib/student'
=> true
  • pry(main)> this is just referencing what you would see in a pry session.
  • require './lib/student' is showing you what you would include at the top of your student_spec.rb file. This would go below the require 'rspec'.
    • => is showing you what the return value of the require './lib/student' is. This symbol will always reference what the expected return value is.

Writing a Test

  1. We will working within our spec directory.
  2. Working with the interaction pattern above, we will be creating a student.
  3. Here is the basic setup for a test:
# student_spec.rb
require 'rspec'
# additional requires will go here

describe Student do

end
  1. Within the describe there will be individual tests. These tests are initiated with a it. Here is what that looks like:
it 'the name of method' do

end
  1. So let's take a look at the the test stages know as S.E.A.T (setup, execution, assertion, teardown):
# student_spec.rb
require 'rspec'

describe Student do
  it 'exists' do
    student = Student.new('Penelope') # Setup
    expect(student).to be_a Student # Execution = student, Assertion = Student
  end

  it "#name" do
    student = Student.new('Penelope') #Setup
    expect(student.name).to eq('Penelope') # Execution = student.name Assertion = 'Penelope'
  end
end

Practice

intro_to_testing's People

Contributors

dcoleman21 avatar megstang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.