Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
brickgao committed Aug 19, 2016
1 parent 87dfdcf commit 2acec41
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 0 additions & 1 deletion app/controllers/nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def create
if @node.save
redirect_to '/'
else
puts @node.errors
render 'new'
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/manage_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ class ManageControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
def setup
user = users(:alice)
session[:user_id] = user.id
end

test "should get successfully" do
get :index
assert_response :success
end
end
33 changes: 33 additions & 0 deletions test/controllers/nodes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,43 @@ class NodesControllerTest < ActionController::TestCase
# assert true
# end

def setup
user = users(:alice)
session[:user_id] = user.id
end

test "should return nodes" do
get :index
assert_response :success
data = Node.all.map { |node| { :id => node.id, :name => node.name } }
assert_equal @response.body, JSON.dump(data)
end

test "should get new successfully" do
get :new
assert_response :success
end

test "should create node successfully" do
post :create, post_params
assert_response :redirect
end

test "should create node unsuccessfully with empty name" do
_post_params = post_params
_post_params[:node][:name] = ''
post :create, _post_params
assert_response :success
assert_select 'div.error-message'
end

private
def post_params
{
:node => {
:name => 'samplename',
:summary => 'samplesummary'
}
}
end
end
2 changes: 2 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
alice:
name: Alice
email: alice@test.com
user_type: 1
password_digest: <%= User.digest('badpassword') %>

bob:
name: Bob
email: bob@test.com
user_type: 0
password_digest: <%= User.digest('anotherbadpassword') %>

0 comments on commit 2acec41

Please sign in to comment.