Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tests and seeds for comments

and several small fixes
  • Loading branch information...
commit 57920581d1f45133cdd031ffeacd3cbb3dea7d5e 1 parent b051a6f
Egor Lynko authored
10 app/views/comments/_comment.html.erb
View
@@ -10,8 +10,10 @@
<div class="text">
<%= comment.text %>
</div>
- <div class="reply">
- <%= link_to 'Reply', new_product_comment_path(comment.product, reply_to: comment),
- onclick: "comment_show_reply_form(#{comment.id}); return false" %>
- </div>
+ <% if can? :create, Comment %>
+ <div class="reply">
+ <%= link_to 'Reply', new_product_comment_path(comment.product, reply_to: comment),
+ onclick: "comment_show_reply_form(#{comment.id}); return false" %>
+ </div>
+ <% end %>
</li>
29 db/seeds.rb
View
@@ -1,7 +1,7 @@
data = [
{
product: {
- title: 'CoffeeScrpt',
+ title: 'CoffeeScript',
description:
%{<p>
CoffeeScript is JavaScript done right. It provides all of JavaScript's
@@ -108,3 +108,30 @@
end
end
end
+
+
+# Comments
+def createComment(product, user, parent = nil)
+ comment = Comment.create!({
+ text: parent.nil? ? "I'm #{user.name} and I think that #{product.title} is #{%w(good perfect bad horrible).sample}" :
+ "I'm #{user.name} and I #{%w(agree disagree).sample} with #{parent.user.name}",
+ product: product,
+ user: user},
+ without_protection: true
+ )
+
+ comment.move_to_child_of(parent) unless parent.nil?
+end
+
+Product.all.each do |product|
+ User.all.each do |user|
+ product.reload
+
+ another_user_comments = product.comments.select { |comment| comment.user != user }
+ another_user_comments.sample(another_user_comments.count / 2).each do |parent_comment|
+ createComment(product, user, parent_comment)
+ end
+
+ createComment(product, user)
+ end
+end
54 spec/controllers/comments_controller_spec.rb
View
@@ -14,21 +14,67 @@
end
it "returns http success" do
- get 'new', {product_id: @product}
- puts response.body
+ get :new, {product_id: @product}
response.should be_success
end
- it "the rest of tests"
+ it "should assign a new comment as @comment" do
+ get :new, {product_id: @product}
+ assigns(:comment).should be_a_new(Comment)
+ end
end
describe "POST 'create'" do
before(:each) do
@ability.can :create, Comment
+ @ability.can :read, Product
+ @user = Factory(:user)
+ @controller.stub(:current_user).and_return(@user)
+ @product = Factory(:product)
+ end
+
+ describe "with valid params" do
+
+ before(:each) do
+ @comment_attr = Factory.attributes_for(:comment)
+ end
+
+ it "should create a new comment" do
+ expect {
+ post :create, product_id: @product.to_param, comment: @comment_attr
+ }.to change(Comment, :count).by(1)
+ end
+
+ it "should assign a newly created comment as @comment" do
+ post :create, product_id: @product.to_param, comment: @comment_attr
+ assigns(:comment).should be_a(Comment)
+ assigns(:comment).should be_persisted
+ end
+
+ it "should redirect to the product" do
+ post :create, product_id: @product.to_param, comment: @comment_attr
+ comment = assigns(:comment)
+ response.should redirect_to(action: 'show', controller: 'products',
+ id: @product.to_param, anchor: "comment-#{comment.id}")
+ end
end
- it "the rest of tests"
+ describe "with invalid params" do
+
+ it "should not create a new comment with invalid params" do
+ expect {
+ post :create, product_id: @product.to_param, comment: nil
+ }.not_to change(Product, :count)
+ end
+
+ it "should re-render the 'new' template" do
+ # Trigger the behavior that occurs when invalid params are submitted
+ Comment.any_instance.stub(:save).and_return(false)
+ post :create, product_id: @product.to_param
+ response.should render_template("new")
+ end
+ end
end
end
6 spec/factories.rb
View
@@ -47,4 +47,10 @@
address "#{Random.rand(10..999)} Some st., Somecity"
pay_type Order::PAYMENT_TYPES.sample
end
+
+ factory :comment do
+ product
+ text "I'm #{user.name} and I approve this message"
+ parent_id nil
+ end
end
46 spec/models/comment_spec.rb
View
@@ -1,5 +1,49 @@
require 'spec_helper'
describe Comment do
- pending "add some examples to (or delete) #{__FILE__}"
+
+ it "should respects mass-assignment security" do
+ should allow_mass_assignment_of(:text)
+ should allow_mass_assignment_of(:parent_id)
+
+ should_not allow_mass_assignment_of(:user_id)
+ end
+
+ it "shoult create a new comment given valid attr" do
+ expect {
+ Factory(:comment)
+ }.to change(Comment, :count).by(1)
+ end
+
+ it "should require a text" do
+ Factory.build(:comment, text: "").should_not be_valid
+ end
+
+ it "should require a product_id" do
+ Factory.build(:comment, product_id: nil).should_not be_valid
+ end
+
+ it "should require a user_id" do
+ Factory.build(:comment, user_id: nil).should_not be_valid
+ end
+
+ describe "nested set" do
+
+ before(:each) do
+ @parent_comment = Factory(:comment)
+ @child_comment = Factory(:comment, parent_id: @parent_comment.id)
+ @parent_comment.reload
+ end
+
+ it "should create child comment" do
+ @child_comment.is_descendant_of?(@parent_comment).should be_true
+ end
+
+ it "should destroy child comment when parent comment was destroyed" do
+ @parent_comment.destroy
+ expect {
+ @child_comment.reload
+ }.to raise_exception ActiveRecord::RecordNotFound
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.