簡體   English   中英

Ruby on Rails 4和RSPEC

[英]Ruby on Rails 4 and RSPEC

我正在嘗試使用Rails和Rspec來使用Capybara進行測試。 我目前正在嘗試測試用戶的個人資料名稱的唯一性。 但是,通過閱讀固定裝置不應與Rspec一起使用。 我如何為我添加一些基礎數據進行測試。

的Gemfile

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.3'

# Use sqlite3 as the database for Active Record
gem 'sqlite3'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.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', '~> 1.2'

gem 'devise', '~> 3.2.3'
gem 'simple_form', '~> 3.0.1'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

group :development do
  gem 'awesome_print'
end

group :development, :test do
  gem 'rspec-rails', '~> 2.0'
end

group :test do
 gem 'capybara', '~> 2.0'
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

Create_spec.rb

require 'spec_helper'

describe "Creating a new user" do
  def create_user(options={})
    options[:first_name] ||= "Adam"
    options[:last_name] ||= "Sackfield"
    options[:profile_name] ||= "Sacki"
    options[:email] ||= "email@email.com"
    options[:password] ||= "password"

    visit "/users/sign_up"
    expect(page).to have_content "Sign up"

    fill_in "First name", with: options[:first_name]
    fill_in "Last name", with: options[:last_name]
    fill_in "Profile name", with: options[:profile_name]
    fill_in "Email", with: options[:email]
    fill_in "Password", with: options[:password]
    fill_in "Password confirmation", with: options[:password]

    click_button "Sign up"
  end

  it "a user can register" do
   create_user
   expect(page).to have_content "You have signed up"
  end

  it "a user must enter a first name" do
    create_user first_name: ""
    expect(page).to_not have_content "You have signed up"
  end

  it "a user must enter a last name" do
    create_user last_name: ""
    expect(page).to_not have_content "You have signed up"
  end
end

謝謝

使用factory_girl_rails gem。

factory_girl_rails提供的Rails集成factory_girl這是一個固定裝置更換為直接的定義的語法,用於多個生成策略支持(保存的情況下,未保存的情況下,屬性的散列,並且存根對象),並支持對同一類中的多個工廠(用戶,admin_user等等),包括工廠繼承。

  1. spec目錄下創建一個文件夾作為factories
  2. spec_helper.rb

    RSpec.configure do | config |
    config.include FactoryGirl :: Syntax :: Methods ##只添加這一行
    結束

  3. spec/factories目錄下創建工廠文件,並定義所需的工廠,如Factory Girl文檔中所示

  4. 在spec文件中,使用此處所示的已定義工廠

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM