Reputation: 602
I am trying to get to grips with Rails and Rspec for testing using Capybara. I’m currently trying to test for uniqueness of a Profile Name for User. However from reading around fixtures should not be used with Rspec. How do I go about adding some base data for me to test against.
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 protected]"
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
Thanks
Upvotes: 0
Views: 164
Reputation: 53048
Use factory_girl_rails gem.
factory_girl_rails
provides Rails integration forfactory_girl
which is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance.
spec
directory as factories
.In spec_helper.rb
,
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods ## Add only this line
end
Create factory files under spec/factories
directory and define the required factories
as shown in Factory Girl Documentation
Upvotes: 2