Last active
October 31, 2016 10:35
-
-
Save yudapc/87dab633da201331cb66 to your computer and use it in GitHub Desktop.
RSpec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Model | |
@user.should have(1).error_on(:username) # Checks whether there is an error in username | |
@user.errors[:username].should include("can't be blank") # check for the error message | |
#Rendering | |
response.should render_template(:index) | |
#Redirecting | |
response.should redirect_to(movies_path) | |
#Capybara Matchers | |
response.body.should have_content("Hello world") | |
response.body.should have_no_content("Hello world") | |
response.body.should have_css("input#movie_title") | |
response.body.should have_css("input#movie_title", :value => "Twelve Angry Men") | |
response.body.should have_css("input", :count => 3) #True if there are 3 input tags in response | |
response.body.should have_css("input", :maximum => 3) # True if there or fewer or equal to 3 input tags | |
response.body.should have_css("input", :minimum => 3) # True if there are minimum of 3 input tags | |
response.body.should have_css("input", :between => 1..3) # True if there 1 to 3 input tags | |
response.body.should have_css("p a", :text => "hello") # True if there is a anchor tag with text hello | |
response.body.should have_css("p a", :text => /[hH]ello(.+)/i) | |
# True if there is a anchor tag with text matching regex | |
response.body.should have_xpath("//a") | |
response.body.should have_xpath("//a",:href => "google.com") | |
response.body.should have_xpath("//a[@href => 'google.com']") | |
response.body.should have_xpath("//a[contains(.,'some string')]") | |
response.body.should have_xpath("//p//a", :text => /re[dab]i/i, :count => 1) | |
# can take both xpath and css as input and can take arguments similar to both have_css and have_xpath | |
response.body.should have_selector(:xpath, "//p/h1") | |
response.body.should have_selector(:css, "p a#movie_edit_path") | |
# For making capybara to take css as default selector | |
Capybara.default_selector = :css | |
response.body.should have_selector("input") #checks for the presence of the input tag | |
response.body.should have_selector("input", :value =>"Twelve Angry Men") # checks for input tag with value | |
response.body.should have_no_selector("input") | |
# For making capybara to take css as default selector | |
Capybara.default_selector = :xpath | |
response.body.should have_selector("//input") #checks for the presence of the input tag | |
response.body.should have_selector("//input", :value =>"Twelve Angry Men") # checks for input tag with value | |
# To access elements inside form | |
response.body.should have_field("FirstName") # checks for presence of a input field named FirstName in a form | |
response.body.should have_field("FirstName", :value => "Rambo") | |
response.body.should have_field("FirstName", :with => "Rambo") | |
response.body.should have_link("Foo") | |
response.body.should have_link("Foo", :href=>"googl.com") | |
response.body.should have_no_link("Foo", :href=>"google.com") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails Rspec | |
add this code in Gemfile: | |
group :development, :test do | |
gem 'faker' | |
gem 'rspec-rails', '~> 3.0' | |
gem 'rspec-collection_matchers' | |
gem 'rspec-its' | |
gem 'shoulda', '~> 3.5' | |
gem 'factory_girl_rails' | |
gem 'database_cleaner' | |
gem 'pry-rails' | |
gem 'pry-nav' | |
gem 'guard-rspec', require: false | |
gem 'ruby-progressbar' | |
gem 'terminal-notifier-guard' | |
gem 'jasmine-rails' | |
gem 'jasmine-jquery-rails' | |
gem 'awesome_print' | |
gem 'bullet' | |
end | |
group :test do | |
gem 'capybara' | |
gem 'selenium-webdriver' | |
gem 'rspec_junit_formatter', git: 'http://github.com/sj26/rspec_junit_formatter' | |
gem 'jasmine-junitreporter' | |
end | |
$ rails g rspec:install | |
when you get error gem mysql2 not loaded you can fix like this in Gemfile: | |
gem 'mysql2', '~> 0.3.20' | |
try to generate model Post | |
$ rails g model Post title description:text content:text user_id:integer published:boolean | |
sample factory: | |
FactoryGirl.define do | |
factory :post do | |
title { Faker::Lorem.sentence(8) } | |
description { Faker::Lorem.sentence(15) } | |
content { Faker::Lorem.sentence(150) } | |
user_id 1 | |
published 1 | |
factory :unpublished_post do | |
after(:create) do |post, evaluator| | |
post.update(published: false) | |
end | |
end | |
end | |
end | |
how to use in rails console | |
$ rails c | |
FactoryGirl.create :post | |
FactoryGirl.create :unpublished_post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#test called method: | |
require 'spec_helper' | |
describe ApplicationHelper do | |
describe '#response_json' do | |
it 'should return json with specify format' do | |
expect_json = { | |
data: {}, | |
meta: { | |
code: 200, | |
errors: {} | |
} | |
} | |
allow(helper).to receive(:response_json).with({}, 200, {}).and_return(expect_json) | |
expect(helper.response_json({}, 200, {})).to eq expect_json | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sample mocking value params | |
describe ApiController do | |
let(:outlet) { create :outlet } | |
describe '#expose_outlet' do | |
context 'when outlet is found' do | |
it "should return outlet" do | |
# mocking value params[:outlet_id] | |
allow(controller).to receive(:params).and_return({outlet_id: outlet.id}) | |
expect(subject.expose_outlet).to eq outlet | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Category do | |
it { is_expected.to have_db_column(:name).of_type(:text) } | |
it { is_expected.to have_db_column(:description).of_type(:text) } | |
it { is_expected.to have_db_column(:business_id).of_type(:integer) } | |
it { is_expected.to have_db_column(:is_deleted).of_type(:boolean) } | |
it { is_expected.to have_db_column(:color_code) } | |
it { is_expected.to have_db_column(:outlet_id).of_type(:integer) } | |
it { is_expected.to have_db_column(:guid) } | |
it { is_expected.to have_db_column(:uniq_id) } | |
it { is_expected.to have_db_column(:synchronized_at) } | |
it { is_expected.to have_db_column(:total).of_type(:float).with_options(default: 0, limit: 8) } | |
#index | |
it { is_expected.to have_db_index(:business_id) } | |
it { is_expected.to have_db_index([:name, :outlet_id]) } | |
#relations | |
it { is_expected.to belong_to(:business) } | |
it { is_expected.to belong_to(:outlet) } | |
it { is_expected.to have_many(:items) } | |
it { is_expected.to have_many(:checkouts).through(:items) } | |
it { is_expected.to have_many(:item_variants).through(:items) } | |
# validations | |
it { is_expected.to validate_presence_of :name } | |
it { is_expected.to validate_presence_of :outlet } | |
it { is_expected.to validate_presence_of :business } | |
describe '.scoping' do | |
describe '.active' do | |
let(:category) { create :category, is_deleted: false } | |
let(:category_notactive) { create :category, is_deleted: true } | |
context 'when category active' do | |
it { expect(Category.active).to include category } | |
it { expect(Category.active).not_to include category_notactive } | |
end | |
end | |
describe '.inactive' do | |
let(:category) { create :category, is_deleted: false } | |
let(:category_notactive) { create :category, is_deleted: true } | |
context 'when category inactive' do | |
it { expect(Category.inactive).not_to include category } | |
it { expect(Category.inactive).to include category_notactive } | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV["RAILS_ENV"] ||= 'test' | |
require File.expand_path("../../config/environment", __FILE__) | |
require 'capybara/rspec' | |
require 'rspec/rails' | |
require 'shoulda/matchers' | |
# require 'declarative_authorization/maintenance' | |
require 'socket' | |
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } | |
ActiveRecord::Migration.maintain_test_schema! | |
SELENIUM_SERVER = '127.0.0.1' | |
CAPYBARA_DEFAULT_HOST = "localhost" | |
Capybara.register_driver :selenium do |app| | |
Capybara::Selenium::Driver.new(app, browser: :remote, url: "http://#{SELENIUM_SERVER}:4444/wd/hub", desired_capabilities: :firefox) | |
end | |
Capybara.javascript_driver = :selenium | |
RSpec.configure do |config| | |
config.infer_base_class_for_anonymous_controllers = false | |
config.infer_spec_type_from_file_location! | |
config.order = "random" | |
config.use_transactional_fixtures = false | |
config.raise_errors_for_deprecations! | |
CAPYBARA_DEFAULT_PORT = 3005 | |
Capybara.default_host = "http://#{CAPYBARA_DEFAULT_HOST}" | |
Capybara.server_port = CAPYBARA_DEFAULT_PORT | |
Capybara.app_host = "http://#{CAPYBARA_DEFAULT_HOST}:#{Capybara.server_port}" | |
config.before :each do |example| | |
if Capybara.current_driver == :rack_test | |
DatabaseCleaner.strategy = :transaction | |
else | |
DatabaseCleaner.strategy = :truncation | |
end | |
DatabaseCleaner.start | |
if example.metadata[:type] == :request && example.metadata[:js].to_s == 'true' | |
page.driver.browser.manage.window.maximize | |
end | |
end | |
config.after do |example| | |
DatabaseCleaner.clean | |
Capybara.app_host = "http://#{CAPYBARA_DEFAULT_HOST}:#{Capybara.server_port}" | |
if example.exception && example.metadata[:type] == :request | |
now = DateTime.now | |
filename = "tmp/" + example.metadata[:file_path].parameterize + "-#{now}.html" | |
File.open(filename, 'w') {|f| f.write Capybara.page.html } | |
end | |
end | |
config.include Capybara::DSL, type: :request | |
# config.include Devise::TestHelpers, type: :controller | |
# config.include Authorization::TestHelper, type: :controller | |
end | |
def mock_paginate(object) | |
pager = double | |
allow(pager).to receive(:per).and_return object | |
allow(object).to receive(:current_page).and_return(1) | |
allow(object).to receive(:total_pages).and_return(1) | |
allow(object).to receive(:limit_value).and_return(1) | |
allow(object).to receive(:page).and_return pager | |
object | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment