gpt4 book ai didi

railstutorial.org - Ruby on Rails 教程第 3 版,第 11 章,练习 3,图像 uploader 测试

转载 作者:行者123 更新时间:2023-12-04 09:01:01 26 4
gpt4 key购买 nike

指令是这样的:

按照 list 11.68 中的模板,编写第 11.4 节中的图像 uploader 测试。作为准备,您应该将图像添加到fixtures目录(使用例如cp app/assets/images/rails.png test/fixtures/)。 (如果你使用 Git,我还建议更新你的 .gitignore 文件,如 list 11.69 所示。)为了避免混淆错误,你还需要配置 CarrierWave 以通过创建一个初始化文件来跳过测试中的图像大小调整,如 list 11.70。 list 11.68 中的附加断言检查主页上的文件上传字段和有效提交产生的​​微博上的有效图像属性。注意使用特殊的fixture_file_upload 方法将文件上传为测试中的fixture。22 提示:要检查有效的图片属性,请使用第10.1.4 节中提到的assigns 方法在有效提交后访问create action 中的微帖子。

这是我在 test/integration/micrposts_interface_test.rb 中的代码

  test "micropost interface" do
log_in_as(@user)
get root_path
assert_select 'div.pagination'
assert_select 'input[type=FILL_IN]'
# Invalid submission
assert_no_difference 'Micropost.count' do
post microposts_path, micropost: { content: "" }
end
assert_select 'div#error_explanation'
# Valid submission
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
# Delete a post.
assert_select 'a', text: 'delete'
first_micropost = @user.microposts.paginate(page: 1).first
assert_difference 'Micropost.count', -1 do
delete micropost_path(first_micropost)
end
# Visit a different user.
get user_path(users(:archer))
assert_select 'a', text: 'delete', count: 0
end

在assert_select 'input[type=FILL_IN]'中,我的填写是type=file

在post microposts_path, micropost: { content: content, picture: FILL_IN },我的填写是picture: true

在断言 FILL_IN.picture 中?是@user.microposts.picture?

我得到的错误是:NoMethodError:NoMethodError:未定义的方法“图片?”对于#

所以我认为我的前两个 FILL_IN 是正确的。问题出在第三次填写。我写@user.microposts 是因为我想测试图片是否存在?它必须在用户的微博上。但是错误是没有图片的方法?

我也试过@user.micropost.picture?,错误是NoMethodError: NoMethodError: undefined method `micropost' for #

我认为我的推理是正确的,但显然不是。帮助!我是个新手。

最佳答案

我的解决方案,它似乎有效:

test "micropost interface" do
log_in_as(@user)
get root_path
assert_select 'div.pagination'
assert_select 'input[type=file]'
# Invalid submission
assert_no_difference 'Micropost.count' do
post microposts_path, micropost: { content: "" }
end
assert_select 'div#error_explanation'
# Valid submission
content = "This micropost really ties the room together"
picture = fixture_file_upload('test/fixtures/sample-image.jpg', 'image/jpg')
assert_difference 'Micropost.count', 1 do
post microposts_path, micropost: { content: content, picture: picture }
end
assert assigns(:micropost).picture?
assert_redirected_to root_url
follow_redirect!
assert_match content, response.body
# Delete a post.
assert_select 'a', text: 'delete'
first_micropost = @user.microposts.paginate(page: 1).first
assert_difference 'Micropost.count', -1 do
delete micropost_path(first_micropost)
end
# Visit a different user.
get user_path(users(:archer))
assert_select 'a', text: 'delete', count: 0
end

关于railstutorial.org - Ruby on Rails 教程第 3 版,第 11 章,练习 3,图像 uploader 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28896562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com