Skip to content

Instantly share code, notes, and snippets.

@cigalecigales
Created February 21, 2015 13:11
Show Gist options
  • Save cigalecigales/2accef9e8c41d7fdc076 to your computer and use it in GitHub Desktop.
Save cigalecigales/2accef9e8c41d7fdc076 to your computer and use it in GitHub Desktop.
[*Rails*] RSpec + devise + Twitter認証 テストメモ ref: http://qiita.com/cigalecigales/items/a98be8416bf169ce5942
FactoryGirl.define do
factory :user do
email "rspec_test@test.com"
uid "123"
provider "twitter"
username "anonymous"
confirmed_at Time.now
end
end
$ rails g rspec:install
$ rails g integration_test twitter_omniauth
$ bundle exec rspec spec/requests/twitter_omniauths_spec.rb
.
Finished in 0.86037 seconds (files took 21.05 seconds to load)
1 example, 0 failures
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
group :development, :test do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
gem 'rspec-rails'
end
group :test do
gem 'selenium-webdriver'
gem 'capybara'
gem 'factory_girl_rails'
end
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
# Devise
gem 'devise', '3.4.1'
gem 'omniauth-twitter'
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
(省略)...
<% if f.object.password_required? %>
<div class="field">
<%= f.label :password %>
<% if @validatable %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<% end %>
(省略)...
<% end %>
<%= render "devise/shared/links" %>
ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../config/environment', __FILE__)
(省略)...
RSpec.configure do |config|
(省略)...
# Add Capybara DSL to rspec helper
config.include Capybara::DSL
end
Rails.application.configure do
(省略)...
# Devise host mail setting
config.action_mailer.default_url_options = { host: 'localhost:3000' }
end
require 'rails_helper'
describe "twitter login test" do
before do
OmniAuth.config.test_mode = true
FactoryGirl.create(:user)
OmniAuth.config.mock_auth[:twitter] = {
"uid" => "123",
"provider" => "twitter",
"info" => {
"nickname" => "anonymous"
}
}
visit user_omniauth_authorize_path(:twitter)
end
after do
OmniAuth.config.test_mode = false
end
describe "after login" do
before{ visit "/pages/show" }
subject{ page }
it{ should have_link('ログアウト') }
end
end
class User < ActiveRecord::Base
(省略)...
def password_required?
super && provider.blank?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment