0

Try to use turnip_support gem

turnip_support

turnip-support is a gem that support testing features of Rails applications using turnip, rspec and headless browser.

Now i will show an example to test Rails application with turnip_support gem.

Design database

  • Role

    • id
    • name
  • User

    • id
    • name
    • login_id
    • role_id
    • password
    • password_confirmation

Create new rails project

rails new turnip_support_example
  • Create models
rails generate scaffold Role name:string
rails generate scaffold User name:string login_id:string role:references password:string password_confirmation:string
  • Run
rake db:create
rake db:migrate
rails s

Create factories for each model

  • Create folder spec/factories
  • Create 2 file with below contents:

spec/factories/users.rb

FactoryGirl.define do
  factory :user do
    sequence(:login_id){|n| "user#{n}"}
    sequence(:name){|n| "Admin User #{n}"}
    role {FactoryGirl.create :role}
    password "12345678"
    password_confirmation "12345678"
  end
end

spec/factories/roles.rb


FactoryGirl.define do
  factory :role do
    sequence(:name){|n| "role-#{n}"}
  end
end

Configuration

  • Add gem "turnip_support", git: "git@github.com:ThuBM/turnip_support.git" to group :test of your Gemfile
  • Run
bundle install
bundle exec rake turnip_support init

Create test case with google spread sheet

  • Create a spread sheet with content like below spread sheet

Test case sample

  • Create file config.json Follow This tutorial Result is a file has content:
{
 "client_id": "xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
 "client_secret": "xxxxxxxxxxxxxxxxxxxxxxxx"
}
  • Move this file to folder spec/configs
  • Create spec/configs/list_user.yml file with below content:
spreadsheet_key: 1IHXpmlxUbkioauITZNgtFoYzlixHh1yKzsbD1w1aQ6Y
config_file: config.json
worksheet_order_number: 0

1IHXpmlxUbkioauITZNgtFoYzlixHh1yKzsbD1w1aQ6Y is test case's spread sheet key. config.json is the name of just created json file what is placed in spec/configs folder. worksheet_order_number is the order number of test case worksheet in the test case spread sheet.

  • Save

Create feature file

  • Run
bundle exec rake turnip_support list_user list_user.yml

with list_user param is the feature name,

list_user.yml is the configuration file what is just created and placed in spec/configs folder.

At the first time, below message will be displayed.

1. Open this page:
https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https://www.googleapis.com/auth/drive%20https://spreadsheets.google.com/feeds/

2. Enter the authorization code shown in the page:

Copy displayed link and paste to browser, you can see the authorization code. Copy this code and paste to terminal then press Enter.

If below error is appeared,

Google::Apis::ClientError: accessNotConfigured: Access Not Configured. Drive API has not been used in project PROJECT_ID before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/drive/overview?project=PROJECT_ID then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

please access to https://console.developers.google.com/apis/api/drive/overview?project=PROJECT_ID and enable the Google Drive API then run again:

bundle exec rake turnip_support list_user list_user.yml

Run testing

bundle exec rspec spec/features/list_user.feature

Reference


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí