Ruby on Rails教程(第4版) 代码清单 13.63 assert FILL_IN.picture?

Mac环境

Rails 5.1.4

ruby 2.4.1

bootstrap v3.3.7

 

Ruby on Rails教程(第4版) 代码清单 13.63 assert FILL_IN.picture?

直接贴出答案

第13章 代码清单 13.63

 1 require 'test_helper'
 2 
 3 class MicropostsInterfaceTest < ActionDispatch::IntegrationTest
 4   def setup
 5     @user = users(:michael)
 6   end
 7 
 8   test "micropost interface" do
 9     log_in_as(@user)
10     get root_path
11     assert_select 'div.pagination'
12     assert_select 'input[type=file]'
13     # 无效提交
14     assert_no_difference 'Micropost.count' do
15       post microposts_path, params: { micropost: { content: "" } }
16     end
17     assert_select 'div#error_explanation'
18     # 有效提交
19     content = "This micropost really ties the room together"
20     picture = fixture_file_upload('test/fixtures/timg.jpeg', 'image/jpeg')
21     assert_difference 'Micropost.count', 1 do
22       post microposts_path, params: { micropost: { content: content, picture: picture } }
23     end
24     assert_redirected_to root_url
25     assert c(:micropost).picture?
26     follow_redirect!
27     assert_match content, response.body
28     # 删除一篇微博
29     assert_select 'a', text: 'delete'
30     first_micropost = @user.microposts.paginate(page: 1).first
31     assert_difference 'Micropost.count', -1 do
32       delete micropost_path(first_micropost)
33     end
34     # 访问另一个用户的资料页面(没有删除链接)
35     get user_path(users(:archer))
36     assert_select 'a', text: 'delete', count: 0
37   end
38 
39   test "micropost sidebar count" do
40     log_in_as(@user)
41     get root_path
42     assert_match "34 microposts", response.body
43     # 这个用户没有发布微博
44     other_user = users(:malory)
45     log_in_as(other_user)
46     get root_path
47     assert_match "0 microposts", response.body
48     other_user.microposts.create!(content: "A micropost")
49     get root_path
50     assert_match "1 micropost", response.body
51   end
52 
53 end

 

posted @ 2017-11-16 23:50  roy0524  阅读(274)  评论(0编辑  收藏  举报