如何测试动作文本? 如何在 YML 中添加动作文本?



如何在动作文本中添加正文?

Post.rb

has_rich_text :body

测试/夹具/帖子.yml

one:
name: <%= Faker::Name.unique.name %>
body: "body"

posts_controller_test.rb

require 'test_helper'
class PostsControllerTest < ActionDispatch::IntegrationTest
setup do
@post = posts(:one)
end
test "should get index" do
get posts_url
assert_response :success
end
end

我运行了帖子控制器测试预期的成功响应,但得到了Minitest::UnexpectedError: ActiveRecord::Fixture::FixtureError: table "posts" has no columns named "body".

使用 rails 6.0, Ruby 2.7.1

创建文件:

测试/夹具/action_text/rich_texts.yml

如果它不存在,则向其添加以下行:

post_one:
record: one (Post)                          # record: record name (in_yml) in association (ModelName)
name: body                                  # has_rich_text :name_of_rich_text_attribute
body: <p>Lorem ipsum dolor sit amet.</p>    # actual rich text content

您还应该从test/fixtures/posts.yml中删除body: "body"

最新更新