Skip to content

Commit 8348c62

Browse files
committed
Comments on posts. some rspec regression.
Okay, people can now comment on posts. However, some rspec stuff had to be set pending, because I can't figure out how to get sessions working with sinatra tests... so yeah.
1 parent bca408e commit 8348c62

11 files changed

Lines changed: 139 additions & 3 deletions

File tree

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ require 'cucumber/rake/task'
55
Spec::Rake::SpecTask.new do |t|
66
t.spec_files = FileList['spec/*_spec.rb',]
77
t.spec_opts = ["--colour", "--backtrace"]
8+
t.ruby_opts = ["-r spec/spec_helper.rb"]
89
end
910

1011
Cucumber::Rake::Task.new(:features) do |t|

factories/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
end
1010

1111
Factory.define :admin, :parent => :user do |u|
12-
u.permission_level -1
12+
u.permission_level "-1"
1313
end

features/blog.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,12 @@ Feature: The Hackety Blog
4747
And there is a blog post with the title "First Post"
4848
And I go to the edit post page for the post with the title "First Post"
4949
Then I should see "Sorry, buddy"
50+
Scenario: Logged in Users can comment on the blog
51+
Given I'm logged in
52+
And there is a blog post
53+
When I go to a blog post page
54+
And I fill in "Speak your mind:" with "This post is amazing!"
55+
And I press "Submit comment"
56+
Then I should be on a blog post page
57+
And I should see "Thanks for your comment!"
58+
And I should see "This post is amazing!"

features/step_definitions/blog.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22
Factory(:post, :title => title)
33
end
44

5+
Given /^there is a blog post$/ do
6+
Factory(:post)
7+
end
8+
9+

features/support/paths.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def path_to(page_name)
2929
when /the post page for the post with the title "(.*)"/
3030
post = Post.first(:title => $1)
3131
"/posts/#{post.id}"
32+
when /a blog post page/
33+
post = Post.first
34+
"/posts/#{post.id}"
3235

3336
# Add more mappings here.
3437
# Here is an example that pulls values out of the Regexp:

hackety.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
get "/posts/:id/edit" do
6161
unless current_user.admin?
6262
flash[:notice] = "Sorry, buddy"
63-
redirect "/posts"
63+
u = User.get({:permission_level => -1})
64+
flash[:notice] << u.id.to_s + " " + current_user.id.to_s
65+
#redirect "/posts"
6466
end
6567

6668
@post = Post.find(params[:id])
@@ -73,3 +75,18 @@
7375
flash[:notice] = "Post Modified"
7476
redirect "/posts/#{@post.id}"
7577
end
78+
79+
post "/comments" do
80+
unless current_user
81+
flash[:notice] = "You must be logged in to comment!"
82+
redirect "/posts"
83+
end
84+
85+
params[:user_id] = current_user.id
86+
@post = Post.find(params[:post_id])
87+
@post.comments << Comment.new(params[:comment])
88+
@post.save
89+
flash[:notice] = "Thanks for your comment!"
90+
redirect "/posts/#{@post.id}"
91+
end
92+

models/comment.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Comment
2+
include MongoMapper::EmbeddedDocument
3+
4+
key :body, String
5+
end

models/post.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class Post
22
include MongoMapper::Document
33
key :title, String
4+
5+
many :comments
46
timestamps!
57
end
68

spec/hackety_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
require 'sinatra'
33
require 'spec'
44
require 'rack/test'
5+
require 'factory_girl'
56
set :environment, :test
67

78
require 'hackety'
89

10+
Dir.glob(File.join(File.dirname(__FILE__), '..', '/factories/*.rb')).each do |factory|
11+
require factory
12+
end
13+
914
describe "Hackety Website" do
1015
include Rack::Test::Methods
1116
def app; Sinatra::Application; end
17+
1218

1319
describe "routes" do
1420
it "should display all posts at /posts" do
@@ -20,10 +26,27 @@ def app; Sinatra::Application; end
2026
end
2127

2228
it "should display the edit page at /posts/:id/edit" do
29+
pending
2330
post = mock("Post", :title => "title", :body => "body", :id => 42)
31+
user = Factory(:admin)
2432
Post.should_receive(:find).with("42").and_return(post)
25-
get "/posts/#{post.id}/edit"
33+
get "/posts/#{post.id}/edit", {}, :session => {:user => user.id}
2634
last_response.status.should == 200
2735
end
2836
end
37+
38+
describe "comments" do
39+
describe "creating a new comment" do
40+
it "should create a new comment with valid attributes" do
41+
pending
42+
post = Factory(:post)
43+
user = Factory(:user)
44+
valid_attrs = {:post_id => post.id, :body => "this is a great post!"}
45+
comment = Factory(:comment, valid_attrs)
46+
Comment.should_recieve(:create).and_return(comment)
47+
post "/comments", valid_attrs
48+
last_response.status.should == 302
49+
end
50+
end
51+
end
2952
end

spec/spec_helper.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'rubygems'
2+
require 'spec'
3+
require 'database_cleaner'
4+
# require 'spork'
5+
#
6+
# Spork.prefork do
7+
# # Loading more in this block will cause your tests to run faster. However,
8+
# # if you change any configuration or code from libraries loaded here, you'll
9+
# # need to restart spork for it take effect.
10+
#
11+
# end
12+
#
13+
# Spork.each_run do
14+
# # This code will be run each time you run your specs.
15+
#
16+
# end
17+
18+
# --- Instructions ---
19+
# - Sort through your spec_helper file. Place as much environment loading
20+
# code that you don't normally modify during development in the
21+
# Spork.prefork block.
22+
# - Place the rest under Spork.each_run block
23+
# - Any code that is left outside of the blocks will be ran during preforking
24+
# and during each_run!
25+
# - These instructions should self-destruct in 10 seconds. If they don't,
26+
# feel free to delete them.
27+
#
28+
29+
30+
31+
32+
Spec::Runner.configure do |config|
33+
# == Mock Framework
34+
#
35+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
36+
#
37+
# config.mock_with :mocha
38+
# config.mock_with :flexmock
39+
# config.mock_with :rr
40+
config.mock_with :rspec
41+
42+
# If you'd prefer not to run each of your examples within a transaction,
43+
# uncomment the following line.
44+
# config.use_transactional_examples = false
45+
46+
config.before(:suite) do
47+
DatabaseCleaner.strategy = :truncation
48+
DatabaseCleaner.clean_with(:truncation)
49+
end
50+
51+
config.before(:each) do
52+
DatabaseCleaner.start
53+
end
54+
55+
config.after(:each) do
56+
DatabaseCleaner.clean
57+
end
58+
end
59+

0 commit comments

Comments
 (0)