GithubHelp home page GithubHelp logo

salamandermike / rspec_mocks_lesson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wdi-sf-july/rspec_mocks_lesson

0.0 2.0 0.0 128 KB

A lesson on TDD for controllers and requests

Ruby 90.52% JavaScript 2.12% CoffeeScript 2.70% CSS 4.66%

rspec_mocks_lesson's Introduction

Application Testing

Rspec Rails Part I

Objectives
Identify the RSpsec directory structure and the purpose of each folder
Identify the purpose and give examples of Controller Specs and related structures mocks, stubs, and spies
Employ RSpec mocks, stubs, and spies to solve common testing problems

RSpec Directory

Typical Spec Folders

  • Model
  • Controller
  • Request

Model Specs

The point of a model spec is to test data validations and expected model behaviors.

###What is a factory?

A factory is a tool that allows us to create objects/data for our database to use for testing. We will stick to using factories to test our models.

We use to do this with fixtures, but factories are better

###How to Install Factory Girl

Add Factory Girl

Add factory girl inside of your development, test group in your gemfile.

In Gemfile

group :development, :test do
  gem 'rspec-rails'
  gem 'factory_girl_rails'
end

In spec/rspec_helper

require "factory_girl_rails"

RSpec.configure do |config|
  config.include  FactoryGirl::Syntax::Methods
end

In Terminal

bundle install

Now when we create a model a spec/factories folder will be created for us, as well as coresponding factory files.

FactoryGirl likes to create files that look like this...

spec/factories/ingredients.rb
spec/factories/recipes.rb
spec/factories/users.rb

Defining a User Factory

FactoryGirl.define do
  sequence :email do |n|
    "user_#{n}@factory.com"
  end

  factory :user do
    email 
    pswrd = "blah"
    password pswrd
    password_confirmation pswrd
  end
end

Using a User Factory In A Spec

Here we will just test that our model passes any validations

require 'rails_helper'

RSpec.describe User, :type => :model do
  it "should have a valid factory" do
    user = FactoryGirl.build(:user) 
    expect(user).to be_valid
  end
end

or using the more nice subject setter in rspec

require 'rails_helper'

RSpec.describe User, :type => :model do

  subject { FactoryGirl.create(:user) }

  it "should have a valid factory" do
    expect(subject).to be_valid
  end
end

Setup FFaker

It would be better if your Factories didn't always create the same kind of objects and had more interesting our more realistic test data.

Thus we use a gem called FFaker to give us better test data.

Setup Gemfile

  group :development, :test do
    gem 'rspec-rails'
    gem 'factory_girl_rails'
    gem 'ffaker'
  end

Defining a User Factory Using FFaker

FactoryGirl.define do
  factory :user do
    email Faker::Internet.email
    pswrd = Faker::Lorem.words(4).join("")
    password pswrd
    password_confirmation pswrd
  end
end

Controller Specs

In a controller we might have not yet implemented the model details we would expect to be testing our application, but we still need to write valid tests that describe the behavior of our controller. The helps others focus on reading our specs to work toward implementing the controller.

Resources

rspec_mocks_lesson's People

Contributors

salamandermike avatar

Watchers

James Cloos avatar  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.