Ruby on Rails 4和RSPEC

时间:2014-03-16 16:22:21

标签: ruby-on-rails ruby 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

由于

1 个答案:

答案 0 :(得分:2)

使用 factory_girl_rails gem。

  

factory_girl_railsfactory_girl提供了Rails集成   这是一个具有简单定义的灯具替代品   语法,支持多种构建策略(已保存的实例,   未保存的实例,属性哈希和存根对象)和支持   对于同一个类的多个工厂(user,admin_user等)   on),包括工厂继承。

  1. spec目录下创建factories
  2. spec_helper.rb

    RSpec.configure do | config |
     config.include FactoryGirl :: Syntax :: Methods ##只添加此行

  3. spec/factories目录下创建工厂文件并定义所需的工厂 如 Factory Girl Documentation

  4. 所示
  5. 在您的规范文件中,使用已显示的工厂 here