Skip to content

Instantly share code, notes, and snippets.

@lakshmananmurugesan
Created August 31, 2016 06:14
Show Gist options
  • Save lakshmananmurugesan/1994c9620b6020e088610bd6cb4d350c to your computer and use it in GitHub Desktop.
Save lakshmananmurugesan/1994c9620b6020e088610bd6cb4d350c to your computer and use it in GitHub Desktop.
Rspec::
rspec
rspec spec/models
rspec spec/controllers/accounts_controller_spec.rb
rails generate rspec:model
/factories /support /models /controllers /features /views /mailers /routing /helpers
Model spec - behavior of model
require "rails_helper"
RSpec.describe User, :type => :model do
it "orders by last name" do
lindeman = User.create!(first_name: "Andy", last_name: "Lindeman")
chelimsky = User.create!(first_name: "David", last_name: "Chelimsky")
expect(User.ordered_by_last_name).to eq([chelimsky, lindeman])
end
end
Controller spec - behavior of controller
require "rails_helper"
RSpec.describe PostsController, :type => :controller do
describe "GET #index" do
it "responds successfully with an HTTP 200 status code" do
get :index
expect(response).to be_success
expect(response).to have_http_status(200)
end
it "renders the index template" do
get :index
expect(response).to render_template("index")
end
it "loads all of the posts into @posts" do
post1, post2 = Post.create!(title: 'one'), Post.create!(title: 'two')
get :index
expect(assigns(:posts)).to match_array([post1, post2])
end
end
end
Request spec - behavior of request/response. folders: /api, /integrations, /requests
require 'rails_helper'
RSpec.describe "home page", :type => :request do
it "displays the user's username after successful login" do
user = User.create!(:username => "jdoe", :password => "secret")
get "/login"
assert_select "form.login" do
assert_select "input[name=?]", "username"
assert_select "input[name=?]", "password"
assert_select "input[type=?]", "submit"
end
post "/login", :username => "jdoe", :password => "secret"
assert_select ".header .username", :text => "jdoe"
end
end
factorygirl & capybara version:
require 'rails_helper'
RSpec.describe "home page", :type => :request do
it "displays the user's username after successful login" do
user = FactoryGirl.create(:user, :username => "jdoe", :password => "secret")
visit "/login"
fill_in "Username", :with => "jdoe"
fill_in "Password", :with => "secret"
click_button "Log in"
expect(page).to have_selector(".header .username", :text => "jdoe")
end
end
Feature spec - tests with simulating browser (Capybara)
require 'rails_helper'
describe 'Admin Login', type: :feature do
let!(:admin_user) { FactoryGirl.create(:admin_user, email: 'admin@example.com', password: 'password', password_confirmation: 'password') }
it 'signs in the admin', js:true do
visit '/administration/admin/login'
fill_in 'admin_user_email', with: 'admin@example.com'
fill_in 'admin_user_password', with: 'password'
click_button 'Login'
expect(page).to have_content 'Dashboard'
end
end
mailer spec - testing mail behavior
require "rails_helper"
RSpec.describe Notifications, :type => :mailer do
describe "notify" do
let(:mail) { Notifications.signup }
it "renders the headers" do
expect(mail.subject).to eq("Signup")
expect(mail.to).to eq(["to@example.org"])
expect(mail.from).to eq(["from@example.com"])
end
it "renders the body" do
expect(mail.body.encoded).to match("Hi")
end
end
end
view spec - testing view
require 'rails_helper'
RSpec.describe "events/show", :type => :view do
it "displays the event location" do
assign(:event, Event.new(:location => "Chicago"))
render
expect(rendered).to include("Chicago")
end
end
Routing spec - Route testing
require 'rails_helper'
RSpec.describe "routing to profiles", :type => :routing do
it "routes /profile/:username to profile#show for username" do
expect(:get => "/profiles/jsmith").to route_to(
:controller => "profiles",
:action => "show",
:username => "jsmith"
)
end
it "does not expose a list of profiles" do
expect(:get => "/profiles").not_to be_routable
end
end
Helper spec - helper testing
require 'rails_helper'
RSpec.describe EventsHelper, :type => :helper do
describe "#link_to_event" do
it "displays the title, and formatted date" do
event = Event.new("Ruby Kaigi", Date.new(2010, 8, 27))
# helper is an instance of ActionView::Base configured with the
# EventsHelper and all of Rails' built-in helpers
expect(helper.link_to_event).to match /Ruby Kaigi, 27 Aug, 2010/
end
end
end
Matchers
be_a_new expect(object).to be_a_new(Widget) =>controller
render_template expect(response).to render_template("new") =>request,controller,view
route_to expect(:get => "/widgets").to route_to(:controller => "widgets", :action => "index") =>controller, route
be_routable expect(:get => "/widgets/1/edit").not_to be_routable =>route
have_http_status expect(response).to have_http_status(201) =>controller,feature,request
describe, it, context, example
describe StringCalculator do
describe ".add" do
context "given an empty string" do
it "returns zero" do
expect(StringCalculator.add("")).to eql(0)
end
end
end
end
-----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment