GithubHelp home page GithubHelp logo

bamboocreativenz / rspec-controller-testing Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pukeko-2015/rspec-controller-testing

0.0 11.0 1.0 133 KB

A template for Rails Rspec controller testing

Ruby 100.00%

rspec-controller-testing's Introduction

#A template for Rails Rspec controller testing

We have used a generic "ItemController" and a subsequent "Item" model, with 'item' objects. Update your controller test to include the appropriate model/controller names.


###New Action This tests the #new action in the controller, we provide some examples for tests, both hitting the database and not.

###Context: with hitting the database

before do
  get :new
end

it "creates a new item" do
  expect(assigns(:item)).to be_a_new(Item)
end

###Context: without hitting the database

let(:item_double) { double("item_double") }
before(:each) do
  allow(Item).to receive(:new).and_return(item_double)
  get :new
end

it "creates a new item" do
  expect(assigns(:item)).to be(item_double)
end

###Create Action This tests the #create action in the controller, we provide some examples for tests, both hitting the database and not.

###Context: with hitting the database

it "creates a new Item" do
  expect {
    post :create, item: FactoryGirl.attributes_for(:item)
  }.to change(Item, :count).by(1)
end

###Context: without hitting the database

let(:item_double) { double("item_double") }

before do
  allow(Item.stub).to receive(:new).and_return(item_double)
  allow(item_double).to receive(:save).and_return(true)
end

it "creates a new item" do
  post :create
  expect(assigns(:item)).to be(item_double)
end

it "redirects to the correct url" do
  post :create
  expect(response).to redirect_to root_url
end

###Edit Action This tests the #edit action in the controller, we provide some examples for tests, both hitting the database and not.

###Context: with hitting the database

let(:item) { Item.create(first_attribute: "My name", second_attribute: 23) }

before do
  get :edit, id: item.id
end

it "finds and returns item with specified id" do
  expect(assigns(:item)).to eq(item)
end

###Context: without hitting the database

let(:item) { double("item", id: 1) }

before do 
  get :edit, id: item.id.to_i
end 

it "finds and returns item with specified id" do
  expect(Item).to recieve(:find).with(item.id.to_i).once.and_return(item)
end

###Update Action This tests the #update action in the controller, we provide some examples for tests, both hitting the database and not.

###Context: with hitting the database

let(:item) { Item.create(first_attribute: "My name", second_attribute: 23) }

it "updates an item with valid params" do
  post :update, id: item, item: { first_attribute: "Updated name", second_attribute: 23 }
  item.reload
  expect(item.first_attribute).to eq("Updated name")
end

###Context: without hitting the database

let(:item) { double("todo") }
let(:attrs) { first_attribute: "Updated name", second_attribute: 23 }

it "updates an item with valid params" do
  allow(Item).to receive(:find).and_return(item)
  expect(item).to recieve(:update_attributes).with(attrs.stringify_keys)
  post :update, id: item, item: attrs
end

it "redirects to item once updated" do
  item = stub_model(Item)
  allow(Item).to receive(:find).and_return(item)
  allow(item).to receive(:update_attributes).and_return(true)
  post :update, id: item, item: attrs
  expect(response).to redirect_to(item)
end

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.