Skip to content

Commit 7614778

Browse files
committed
tests are back
1 parent 9569f59 commit 7614778

12 files changed

Lines changed: 375 additions & 2 deletions

Rakefile

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
# The Rakefile only has one task. The description is pretty good.
1+
# This file sets up all of our rake tasks
2+
require 'rubygems'
3+
require 'bundler'
4+
Bundler.setup
5+
6+
require 'rspec/core/rake_task'
7+
8+
29
desc "Generate documentation, and put it in the gh-pages branch"
310
task :dox do
411
# First, we want to stash any changes. We only want stuff that's committed!
@@ -34,3 +41,20 @@ task :dox do
3441
sh "git checkout master"
3542
system "git stash pop"
3643
end
44+
45+
46+
desc 'Run the code in spec'
47+
RSpec::Core::RakeTask.new do |t|
48+
t.pattern = "spec/**/*_spec.rb"
49+
t.rspec_opts = "-r ./spec/spec_helper.rb"
50+
end
51+
52+
namespace :spec do
53+
54+
desc "Run the code examples in spec/acceptance"
55+
RSpec::Core::RakeTask.new(:acceptance) do |t|
56+
t.pattern = "spec/acceptance/**/*_spec.rb"
57+
t.rspec_opts = "-r spec/spec_helper.rb"
58+
end
59+
60+
end

helpers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def require_login!(opts = {:return => "/"})
5252
# site. For this, we allow a username and password to be passed in, and we
5353
# authenticate directly, rather than relying on a previously logged in
5454
# session.
55-
def require_login_or_api!(opts = {:return => "/"})
55+
def require_login_or_api!(opts={})
56+
return if session[:hacker_id]
5657
hacker = Hacker.authenticate(opts[:username], opts[:password])
5758
if hacker
5859
session[:hacker_id] = hacker.id

spec/.gitignore

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'capybara/dsl'
2+
3+
RSpec.configure do |config|
4+
config.include Capybara
5+
end
6+
7+
set :environment, :test
8+
9+
app_file = File.join(File.dirname(__FILE__), '..', '..', 'hackety.rb')
10+
require app_file
11+
Sinatra::Application.app_file = app_file
12+
Capybara.app = Sinatra::Application
13+
14+
Capybara.save_and_open_page_path = '/tmp'
15+
Capybara.javascript_driver = :culerity
16+
17+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each {|f| require f}

spec/acceptance/hacker_spec.rb

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
require File.dirname(__FILE__) + '/acceptance_helper'
2+
3+
feature "Hackers" do
4+
5+
scenario "can register" do
6+
7+
visit '/signup'
8+
9+
fill_in "Email", :with => "steve@example.com"
10+
fill_in "Username", :with => "steve"
11+
fill_in "Password", :with => "foobar"
12+
fill_in "Confirm Password", :with => "foobar"
13+
click_button "Create account"
14+
15+
page.should have_content "Account created."
16+
should_be_on "/"
17+
end
18+
19+
scenario "have visible profiles" do
20+
hacker = Factory(:hacker, :username => "steve")
21+
visit "/hackers/steve"
22+
page.should have_content "steve's page"
23+
end
24+
25+
scenario "can't send messages to themselves" do
26+
@hacker = Factory(:hacker)
27+
log_in @hacker
28+
29+
visit "/"
30+
click_link "My Page"
31+
should_be_on "/hackers/#{@hacker.username}"
32+
page.should_not have_content "Send steve a message"
33+
end
34+
35+
scenario "can follow other hackers" do
36+
@fela = Factory(:hacker, :username => "fela")
37+
38+
@steve = Factory(:hacker, :username => "steve")
39+
log_in @steve
40+
41+
visit "/hackers/fela"
42+
click_link "Follow fela"
43+
44+
page.should have_content "Now following fela."
45+
should_be_on "/hackers/steve"
46+
page.should have_content "Following: 1"
47+
48+
visit "/hackers/fela"
49+
page.should have_content "Followers: 1"
50+
end
51+
52+
scenario "can see followers" do
53+
@fela = Factory(:hacker, :username => "fela")
54+
@steve = Factory(:hacker, :username => "steve")
55+
56+
@steve.follow! @fela
57+
58+
visit "/hackers/fela"
59+
60+
click_link "1"
61+
page.should have_content "steve"
62+
end
63+
64+
scenario "can see others following" do
65+
@fela = Factory(:hacker, :username => "fela")
66+
@steve = Factory(:hacker, :username => "steve")
67+
@steve.follow! @fela
68+
69+
visit "/hackers/steve"
70+
click_link "1"
71+
page.should have_content "fela"
72+
end
73+
74+
scenario "can unfollow other hackers" do
75+
@steve = Factory(:hacker, :username => "steve")
76+
log_in @steve
77+
78+
@fela = Factory(:hacker, :username => "fela")
79+
@steve.follow! @fela
80+
81+
visit "/hackers/fela"
82+
click_link "Unfollow fela"
83+
should_be_on "/hackers/steve"
84+
85+
page.should have_content "No longer following fela."
86+
page.should have_content "Following: 0"
87+
end
88+
89+
end

spec/acceptance/homepage_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require File.dirname(__FILE__) + '/acceptance_helper'
2+
3+
feature "Homepage" do
4+
5+
scenario "displays a download link" do
6+
visit "/"
7+
page.should have_content "Download v"
8+
end
9+
10+
scenario "provides a link to help" do
11+
visit "/"
12+
click_link "Ask a question"
13+
should_be_on "/stream"
14+
end
15+
16+
end

spec/acceptance/messages_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require File.dirname(__FILE__) + '/acceptance_helper'
2+
3+
feature "Messages" do
4+
5+
scenario "can be sent" do
6+
Pony.should_receive(:deliver)
7+
8+
@hacker = Factory(:hacker)
9+
log_in @hacker
10+
11+
@fela = Factory(:hacker, :username => "fela")
12+
13+
visit "/hackers/fela"
14+
15+
click_link "Send fela a message"
16+
fill_in "Message text", :with => "Hello, fela!"
17+
18+
click_button "Send message"
19+
20+
should_be_on "/hackers/fela"
21+
page.should have_content "Message sent."
22+
end
23+
24+
25+
scenario "can only be sent when logged in" do
26+
@hacker = Factory(:hacker)
27+
visit "/logout"
28+
29+
@fela = Factory(:hacker, :username => "fela")
30+
31+
visit "/hackers/fela"
32+
page.should_not have_content "Send fela a message"
33+
end
34+
35+
scenario "can only see messages sent to me" do
36+
Pony.should_receive(:deliver)
37+
@steve = Factory(:hacker, :username => "steve")
38+
@fela = Factory(:hacker, :username => "fela")
39+
40+
Factory(:message,
41+
:sender => @steve.username,
42+
:recipient => @fela.username,
43+
:body => "Hello, fela!")
44+
45+
visit "/logout"
46+
47+
log_in @fela
48+
49+
visit "/messages"
50+
51+
page.should have_content "Hello, fela!"
52+
page.should have_content "From: steve"
53+
end
54+
55+
scenario "has an inbox link" do
56+
pending "Inbox link no longer exists! TODO!"
57+
@hacker = Factory(:hacker)
58+
log_in @hacker
59+
60+
page.should have_content "Inbox"
61+
end
62+
end

spec/acceptance/programs_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require File.dirname(__FILE__) + '/acceptance_helper'
2+
3+
feature "Programs" do
4+
5+
scenario "can be created" do
6+
@hacker = Factory(:hacker)
7+
log_in @hacker
8+
9+
visit "/programs/new"
10+
fill_in "Title", :with => "Hello world"
11+
fill_in "Body", :with => "puts 'Hello, world!'"
12+
click_button "Create"
13+
14+
should_be_on "#{@hacker.username}/hello_world"
15+
16+
page.should have_content "Program created"
17+
page.should have_content "Hello world"
18+
page.should have_content "By steve"
19+
page.should have_content "puts"
20+
21+
end
22+
23+
scenario "can be seen on my page" do
24+
@hacker = Factory(:hacker)
25+
log_in @hacker
26+
27+
(rand(5) + 1).times do |n|
28+
@hacker.programs << Factory(:program)
29+
end
30+
31+
visit "/hackers/#{@hacker.username}"
32+
page.should have_content "Programs"
33+
34+
@hacker.programs.each do |program|
35+
page.should have_content program.title
36+
end
37+
end
38+
end

spec/acceptance/support/helpers.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def should_be_on(path)
2+
page.current_url.should match(Regexp.new(path))
3+
end
4+
5+
def should_not_be_on(path)
6+
page.current_url.should_not match(Regexp.new(path))
7+
end
8+
9+
def log_in(hacker)
10+
visit "/login"
11+
fill_in "username", :with => hacker.username
12+
fill_in "password", :with => hacker.password
13+
click_button "Log in"
14+
end

spec/factories.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#These are the factory_girl factories, for testing.
2+
#Learn more about factory_girl here: http://github.com/thoughtbot/factory_girl
3+
4+
#this is an email sequence, so we don't use the same email over and over!
5+
Factory.sequence :email do |n|
6+
"user#{n}@example.com"
7+
end
8+
9+
#this factory defines a hacker
10+
Factory.define :hacker do |u|
11+
u.username "steve"
12+
u.email { Factory.next(:email) }
13+
u.password "foobar"
14+
#we need to set the password_confirmation to the same value as the password
15+
u.password_confirmation {|user| user.password }
16+
u.admin false
17+
end
18+
19+
Factory.define :message do |m|
20+
end
21+
22+
Factory.sequence :program_title do |n|
23+
"Program #{n}"
24+
end
25+
Factory.define :program do |p|
26+
p.title { Factory.next(:program_title) }
27+
p.code "puts 'hello world'"
28+
end
29+
30+
Factory.define :discussion do |d|
31+
d.forum "learning_ruby"
32+
d.title "A discussion"
33+
d.author "jane_hacker"
34+
d.author_email "jane_hacker@example.com"
35+
end
36+
Factory.define :discussion_with_reply, :parent => :discussion do |t|
37+
t.replies {|r| [Factory(:reply)] }
38+
end
39+
40+
Factory.define :reply do |r|
41+
r.body "I'm here to help!"
42+
r.author "john_hacker"
43+
r.author_email "john_hacker@example.com"
44+
end
45+

0 commit comments

Comments
 (0)