Created
September 30, 2013 16:11
-
-
Save papricek/6766165 to your computer and use it in GitHub Desktop.
How to setup an easy test suite.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./test/factories.rb | |
FactoryGirl.define do | |
sequence :email do |n| | |
Faker::Internet.email | |
end | |
sequence :name do |n| | |
Faker::Lorem.words(3).join(" ").capitalize | |
end | |
sequence :host do |n| | |
"host#{n}.test" | |
end | |
factory :user do | |
password "this_rocks" | |
password_confirmation "this_rocks" | |
factory :confirmed_user do | |
after_create { |user| user.confirm! } | |
factory :confirmed_admin do | |
admin true | |
end | |
end | |
end | |
factory :product do | |
name | |
perex { |p| "This is test product named #{p.name}" } | |
description "Test product is very testable and should be bought very often." | |
state "published" | |
sequence(:price) { |i| (i %100) + 100 } | |
factory :product_new do | |
state "new" | |
end | |
end | |
factory :category do | |
title { FactoryGirl.generate(:name) } | |
description { Faker::Lorem.paragraph(2) } | |
end | |
factory :page do | |
title { FactoryGirl.generate(:name) } | |
text { Lorem::Base.new('paragraphs', 3).output } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./Gemfile | |
... | |
group :test do | |
gem "shoulda-context" | |
gem "shoulda-matchers" | |
gem "factory_girl_rails" | |
gem "mocha" | |
gem "ffaker" | |
gem "test-unit-capybara" | |
gem "launchy" | |
end | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#./test/integration/shop_test.rb | |
require "test_helper" | |
class ShopTest < ActionDispatch::IntegrationTest | |
test "shopping without registration" do | |
category = FactoryGirl.create(:category) | |
discounted_product = FactoryGirl.create(:product, | |
discounted: true, | |
price: 100) | |
product = FactoryGirl.create(:product, | |
category: category, | |
mainpage: false, | |
price: 150) | |
visit "/" | |
assert page.has_content?(discounted_product.name) | |
refute page.has_content?(product.name) | |
click_link(category.title) | |
assert page.has_content?(category.title) | |
assert page.has_content?(discounted_product.name) | |
assert page.has_content?(product.name) | |
click_button('přidat do košíku') | |
assert page.has_content?('150 Kč') | |
assert page.has_content?('260 Kč') # with shipping | |
fill_in "delivery_address_name", :with => 'Milan' | |
fill_in "delivery_address_street", :with => 'Ulice' | |
fill_in "delivery_address_city", :with => 'Prague' | |
fill_in "delivery_address_zip", :with => '14000' | |
fill_in "delivery_address_phone", :with => '777888999' | |
check "billing_address_is_same_as_delivery_address" | |
fill_in "note", :with => 'Note short' | |
click_button('odeslat objednávku') | |
assert page.has_content?('Email není platná hodnota') | |
assert page.has_content?('Email je povinná položka') | |
fill_in "email", :with => '[email protected]' | |
click_button('odeslat objednávku') | |
assert page.has_content?('Objednávka odeslána') | |
order = Order.last | |
assert_equal 'Milan', order.delivery_address.name | |
assert_equal 'Milan', order.billing_address.name | |
assert_equal 260, order.total.to_i | |
assert order.note.include?('Note short') | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./test/test_helper.rb | |
ENV["RAILS_ENV"] = "test" | |
require File.expand_path(File.dirname(__FILE__) + "/../config/environment") | |
require "rails/test_help" | |
require "capybara/rails" | |
class ActiveSupport::TestCase | |
def login_as(user) | |
sign_in user ? (user.is_a?(User) ? user : Factory(user)) : nil | |
end | |
def current_user | |
warden.instance_variable_get(:@users)[:user] | |
end | |
def assert_state(obj, state) | |
assert obj && state, "Object #{obj.inspect} and state #{state.inspect} must not be null." | |
assert obj.send((state.to_s + "?").to_sym), "#{obj.class} shoud be #{state} but is #{obj.state}." | |
end | |
def assert_save(obj) | |
assert obj.save, "Errors: #{pretty_error_messages obj}" | |
obj.reload | |
end | |
def assert_valid(obj) | |
assert obj.valid?, "Errors: #{pretty_error_messages obj}" | |
end | |
end | |
ActionController::TestCase.send :include, Devise::TestHelpers | |
ActionController::TestCase.class_eval do | |
setup do | |
@request.env["devise.mapping"] = Devise.mappings[:user] | |
end | |
end | |
class ActionDispatch::IntegrationTest | |
include Capybara::DSL | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment