Skip to content

Commit

Permalink
Mo/more tests (#32)
Browse files Browse the repository at this point in the history
* remove test files from coverage

* more test coverage
  • Loading branch information
malcolmohare authored May 20, 2024
1 parent 50fefe4 commit dad8597
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions rails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
get '/collections/:id/uncollect', to: 'collections#uncollect', as: 'uncollect_collection'
get '/collections/:id/bulk_create_items', to: 'collections#bulk_create_items', as: 'bulk_create_items'
post '/collections/:id/bulk_create_items', to: 'collections#process_bulk_create_items', as: 'process_bulk_create_items'
get '/items/:id/collect', to: 'items#collect'
get '/items/:id/uncollect', to: 'items#uncollect'
get '/items/:id/collect', to: 'items#collect', as: 'collect_item'
get '/items/:id/uncollect', to: 'items#uncollect', as: 'uncollect_item'
get '/admin', to: 'admin#index'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

Expand Down
74 changes: 74 additions & 0 deletions rails/test/controllers/items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,78 @@ def setup
@item.reload
assert_equal "New Name", @item.name
end

test "should show item" do
get item_url(@item)
assert_response :success
end

test "should get new" do
sign_in @user
get new_item_url
assert_response :success
end

test "should create item" do
sign_in @user
assert_difference('Item.count') do
post items_url, params: { item: { name: "New Item", collection_id: @item.collection_id } }
end

assert_redirected_to collection_url(@item.collection)
end

test "should not create item if not logged in" do
assert_no_difference('Item.count') do
post items_url, params: { item: { name: "New Item", collection_id: @item.collection_id } }
end

assert_redirected_to new_user_session_url
end

test "should get edit" do
sign_in @user
get edit_item_url(@item)
assert_response :success
end

test "should not get edit if not authorized" do
sign_in users(:two)
get edit_item_url(@item)
assert_redirected_to items_url
assert_not_nil flash[:notice]
end

test "should update item" do
sign_in @user
patch item_url(@item), params: { item: { name: "Updated Item" } }
assert_redirected_to item_url(@item)
@item.reload
assert_equal "Updated Item", @item.name
end

test "should not update item if not authorized" do
sign_in users(:two)
patch item_url(@item), params: { item: { name: "Updated Item" } }
assert_redirected_to items_url
assert_not_nil flash[:notice]
end

test "should collect item" do
item = items(:two)
sign_in @user
assert_difference('UserItem.count') do
get collect_item_url(item)
end
assert_response :success
end

test "should uncollect item" do
sign_in @user
UserItem.create(user: @user, item: @item)
assert_difference('UserItem.count', -1) do
get uncollect_item_url(@item)
end
assert_response :success
end
end
4 changes: 3 additions & 1 deletion rails/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ class ActiveSupport::TestCase
fixtures :all

# Add more helper methods to be used by all tests here...
SimpleCov.start
SimpleCov.start do
add_filter "/test/"
end
end

0 comments on commit dad8597

Please sign in to comment.