11.6 练习
最后更新于:2022-04-01 22:30:31
# 11.6 练习
电子书中有练习的答案,如果想阅读参考答案,请[购买电子书](http://railstutorial-china.org/#purchase)。
避免练习和正文冲突的方法参见[第 3 章练习](chapter3.html#mostly-static-pages-exercises)中的说明。
1. 重构首页视图,把 `if-else` 语句的两个分支分别放到单独的局部视图中。
2. 为侧边栏中的微博数量编写测试(还要检查使用了正确的单复数形式)。可以参照[代码清单 11.67](#listing-sidebar-micropost-count)。
3. 以[代码清单 11.68](#listing-image-upload-test) 为模板,为 [11.4 节](#micropost-images)的图片上传程序编写测试。测试之前,要在固件文件夹中放一个图片(例如,可以执行 `cp app/assets/http://railstutorial-china.org/book/images/rails.png test/fixtures/` 命令)。(如果使用 Git,建议你更新 `.gitignore` 文件,如[代码清单 11.69](#listing-gitignore-uploads) 所示。)为了避免出现难以理解的错误,还要配置 CarrierWave,在测试中不调整图片的尺寸。方法是创建一个初始化脚本,写入[代码清单 11.70](#listing-skip-resize-initializer) 中的内容。[代码清单 11.68](#listing-image-upload-test) 中添加的几个断言,检查首页有没有文件上传字段,以及成功提交表单后有没有正确设定 `picture` 属性的值。注意,在测试中上传固件中的文件使用的是专门的 `fixture_file_upload` 方法。[[17](#fn-17)]提示:为了检查 `picture` 属性的值,可以使用 [10.1.4 节](chapter10.html#activation-test-and-refactoring)介绍的 `assigns` 方法,在提交成功后获取 `create` 动作中的 `@micropost` 变量。
##### 代码清单 11.67:侧边栏中微博数量的测试模板
test/integration/microposts_interface_test.rb
```
require 'test_helper'
class MicropostInterfaceTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
.
.
.
test "micropost sidebar count" do
log_in_as(@user)
get root_path
assert_match "#{FILL_IN} microposts", response.body
# 这个用户没有发布微博
other_user = users(:malory)
log_in_as(other_user)
get root_path
assert_match "0 microposts", response.body
other_user.microposts.create!(content: "A micropost")
get root_path
assert_match FILL_IN, response.body
end
end
```
##### 代码清单 11.68:图片上传测试的模板
test/integration/microposts_interface_test.rb
```
require 'test_helper'
class MicropostInterfaceTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "micropost interface" do
log_in_as(@user)
get root_path
assert_select 'div.pagination'
assert_select 'input[type=FILL_IN]' # 无效提交
post microposts_path, micropost: { content: "" }
assert_select 'div#error_explanation'
# 有效提交
content = "This micropost really ties the room together"
picture = fixture_file_upload('test/fixtures/rails.png', 'image/png') assert_difference 'Micropost.count', 1 do
post microposts_path, micropost: { content: content, picture: FILL_IN } end
assert FILL_IN.picture? follow_redirect!
assert_match content, response.body
# 删除一篇微博
assert_select 'a', 'delete'
first_micropost = @user.microposts.paginate(page: 1).first
assert_difference 'Micropost.count', -1 do
delete micropost_path(first_micropost)
end
# 访问另一个用户的资料页面
get user_path(users(:archer))
assert_select 'a', { text: 'delete', count: 0 }
end
.
.
.
end
```
##### 代码清单 11.69:在 `.gitignore` 中添加存储上传图片的文件夹
```
# See https://help.github.com/articles/ignoring-files for more about ignoring
# files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Ignore bundler config.
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
# Ignore Spring files.
/spring/*.pid
# Ignore uploaded test images. /public/uploads
```
##### 代码清单 11.70:一个初始化脚本,在测试中不调整图片的尺寸
config/initializers/skip_image_resizing.rb
```
if Rails.env.test?
CarrierWave.configure do |config|
config.enable_processing = false
end
end
```
';