Skip to content

Instantly share code, notes, and snippets.

@alameenkhader
Last active October 7, 2015 12:51
Show Gist options
  • Save alameenkhader/0f24a191c1d9f8991319 to your computer and use it in GitHub Desktop.
Save alameenkhader/0f24a191c1d9f8991319 to your computer and use it in GitHub Desktop.
More - Rspec

Rspec ??

###Read again

###Gemfile

group :development, :test do
  gem 'rspec-rails'
  gem 'factory_girl_rails'
  gem 'capybara'
  gem 'poltergeist'
  gem 'launchy'
  gem 'database_cleaner'
  gem 'capybara-screenshot'
  gem 'faker'
end

Try this too https://github.com/thoughtbot/formulaic

###If you want to upload files via factories

include ActionDispatch::TestProcess

FactoryGirl.define do
  factory :client do
    name 'Asco'
    description 'Test description'
    domain_name 'demo.example.com'
    email '[email protected]'
    phone '1234567890'
    logo do
     fixture_file_upload(
       Rails.root.join('spec', 'support', 'fixtures', 'new.png'), 'image/png'
     )
   end
  end
end

Do not forget to add include ActionDispatch::TestProcess at the top of the file

###To upload files via APIs

Rack::Test::UploadedFile.new(
   Rails.root.join('spec', 'support', 'fixtures', 'audio-sample.mp3'),
   'image/png'
)

Wait! if you are using carrier wave for file uploading, you should check this out http://icebergist.com/posts/rspec-and-factorygirl-setup-for-testing-carrierwave-uploaders/

More ways to use Except

  • expect(page).to have_content("Credits: #{credits}")
  • expect(page).to have_css('.panel-collapse.in')
  • expect { model.save! }.to raise_error Mongoid::Errors::DocumentNotFound
  • expect(exp_url).to eq(redirect_url)
  • expect(page).to have_xpath(".//img[@src='#{image_url}']")
  • expect(page).to have_link('Google', href: 'google.com')
  • expect(find('.wrap-preview')).to have_text(date)

Log in

  • feature spec - login_as user
  • controller spec - sign_in user

###How to use contexts In spec/supports/my_context.rb

shared_context 'my_context' do
  // some code here
end

Use this context in any feature test include_context 'test_eval_step' If you want to override some thing from the context then

include_context 'test_eval_step' do
  // override here
end

###Using jQuery?? Wait for AJAX?? In spec/support/wait_for_ajax.rb

module WaitForAjax
  def wait_for_ajax
    Timeout.timeout(Capybara.default_wait_time) do
      loop until finished_all_ajax_requests?
    end
  end

  def finished_all_ajax_requests?
    page.evaluate_script('jQuery.active').zero?
  end
end

RSpec.configure do |config|
  config.include WaitForAjax, type: :feature
end

Use it in your feature specs

scenario 'Purchase an activity', js: true do
  visit activity_activity_step_path(activity, overview_step)
  click_link 'Purchase Activity'
  wait_for_ajax
  expect(page).to have_content('Purchased')
end

need angular support? here you go

# spec/support/wait_for_ajax.rb
module WaitForAjax
  def wait_for_ajax
    Timeout.timeout(Capybara.default_wait_time) do
      loop until finished_all_ajax_requests? && finished_angular_requests?
    end
  end

  def finished_all_ajax_requests?
    page.evaluate_script('jQuery.active').zero?
  end

  def finished_angular_requests?
    page.evaluate_script '(typeof angular === "undefined") || (angular.element(".ng-scope").injector().get("$http").pendingRequests.length == 0)'
  end
end

RSpec.configure do |config|
  config.include WaitForAjax, type: :feature
end

###Use the following on top of all your feature spec files (Not sure what it is actually doing :-P )

require 'rails_helper'
include Warden::Test::Helpers
include ActionView::Helpers::SanitizeHelper

###Find the last using xpath

within(:xpath, '(//div[@class="nested-fields"])[last()]') do
  fill_in 'Email:', with: client_b_attr[:email]
  fill_in 'Password:', with: client_b_attr[:password]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment