Skip to content

Instantly share code, notes, and snippets.

@eternal44
Last active September 2, 2015 17:24
Show Gist options
  • Save eternal44/5e139f02b47dbaf9a0a4 to your computer and use it in GitHub Desktop.
Save eternal44/5e139f02b47dbaf9a0a4 to your computer and use it in GitHub Desktop.
TDD practice - www.jamesyoun.com
require 'test_helper'
class RoutesTest < ActionDispatch::IntegrationTest
test 'should create new post' do
assert_routing({ path: 'posts', method: :post }, { controller: 'posts',
action: 'create' })
end
test 'should get post' do
assert_routing '/posts/1', { controller: 'posts', action: 'show', id: '1' }
end
end
# == Schema Information
#
# Table name: posts
#
# id :integer not null, primary key
# title :string
# text :text
# created_at :datetime not null
# updated_at :datetime not null
# user_id :integer
#
require 'test_helper'
class PostTest < ActiveSupport::TestCase
def setup
@post = posts(:valid_post)
end
test 'should be valid' do
assert @post.valid?
end
test 'title should be present' do
@post.title = ""
assert_not @post.valid?
end
test 'text should be present' do
@post.text = ""
assert_not @post.valid?
end
test 'most recent should be first' do
assert_equal posts(:most_recent), Post.first
end
end
require 'test_helper'
class PostPolicyTest < PolicyTest
def setup
@regular = users(:steve)
@admin = users(:james)
end
# check test_helper.rb for Pundit test methods
test 'should create post if admin' do
assert permit(@admin, Post.new, :new)
end
test 'should not create post if regular user' do
assert_not permit(@regular, Post.new, :new)
end
end
require 'test_helper'
require 'database_cleaner'
class PostsTest < ActionDispatch::IntegrationTest
# for setup and tear down each time we run tests
self.use_transactional_fixtures = false
setup do
# comment in if you want to run tests with selenium
# Capybara.default_driver = :selenium
end
def setup
@post = posts(:valid_post)
@admin = users(:james)
@regular = users(:steve)
end
test 'display comments' do
assert @post.comments.first.body == 'hello'
end
test 'visit contact page' do
visit '/contact'
assert page.has_text?('jamesyoun710')
end
test 'login successfully' do
visit new_user_session_path
assert page.has_field?('Email', type: 'email')
assert page.has_content?('Forgot your password?')
fill_in('Email', with: '[email protected]')
fill_in('Password', with: 'password1234')
find_button('Log in')
click_on 'Log in'
assert page.has_content?('Logout'), 'not logged in'
end
test 'create post as admin' do
login_as(@admin)
visit root_path
assert page.has_link?('Logout')
assert page.has_link?('New Post')
click_on('New Post')
assert page.has_field?('Title', type: 'text')
fill_in('Title', with: 'Test Post')
fill_in('Text', with: 'Just another post')
click_on('Create Post')
assert page.has_content?('Just another post')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment