Skip to content

Commit 23e7575

Browse files
committed
Fix tests (with 100% coverage)
1 parent c5e1177 commit 23e7575

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

source/test/controllers/urls_controller_test.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
class UrlsControllerTest < ActionController::TestCase
44
setup do
5-
@url = urls(:one)
5+
@valid_url = urls :valid
6+
@invalid_url = urls :invalid
67
end
78

89
test "should get index" do
@@ -16,22 +17,36 @@ class UrlsControllerTest < ActionController::TestCase
1617
assert_response :success
1718
end
1819

19-
test "should create url" do
20+
test "should create valid url" do
2021
assert_difference('Url.count') do
21-
post :create, url: { click_count: @url.click_count, long_url: @url.long_url, short_url: @url.short_url }
22+
post :create, url: { click_count: @valid_url.click_count, long_url: @valid_url.long_url, short_url: @valid_url.short_url }
2223
end
2324

24-
assert_redirected_to url_path(assigns(:url))
25+
assert_redirected_to urls_url
2526
end
2627

27-
test "should show url" do
28-
get :show, id: @url
29-
assert_response :success
28+
test "should not create invalid url" do
29+
assert_no_difference('Url.count') do
30+
post :create, url: { click_count: @invalid_url.click_count, long_url: @invalid_url.long_url, short_url: @invalid_url.short_url }
31+
end
32+
end
33+
34+
test "should show valid url" do
35+
get :show, id: @valid_url
36+
assert_response :redirect
37+
assert_redirected_to @valid_url.long_url
38+
end
39+
40+
test "should not show nonexistent url" do
41+
assert_raise ActionController::RoutingError do
42+
get :show, id: 'nonexistent'
43+
assert_response :missing
44+
end
3045
end
3146

32-
test "should destroy url" do
47+
test "should destroy valid url" do
3348
assert_difference('Url.count', -1) do
34-
delete :destroy, id: @url
49+
delete :destroy, id: @valid_url
3550
end
3651

3752
assert_redirected_to urls_path

source/test/fixtures/urls.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
22

3-
one:
4-
short_url: MyString
5-
long_url: MyString
6-
click_count: 1
3+
valid:
4+
short_url: google
5+
long_url: https://www.google.com/
6+
click_count: 0
77

8-
two:
8+
invalid:
99
short_url: MyString
1010
long_url: MyString
1111
click_count: 1

0 commit comments

Comments
 (0)