在与Capybara一起发送表单时,Newline会被添加到前面

con*_*com 5 ruby ruby-on-rails capybara

我在使用Capybara的Rails应用程序中进行了此测试:

within "#register" do
  fill_in "Biography (optionnal)", :with => "Hello world!"
end

click_on "Save"

# Check that form is repopulated with old input
expect(find_field('user_bio').value).to eq('Hello world!')
Run Code Online (Sandbox Code Playgroud)

这是我从测试中得到的:

Failure/Error: expect(find_field('user_bio').value).to eq('Hello world!')   
expected: "Hello world!"
got: "\nHello world!"
Run Code Online (Sandbox Code Playgroud)

我从来没有手动将新行添加到user_bio字段.

这可能来自哪里?

编辑1: 经过一些谷歌搜索,似乎在Github上有一个PR,并且它已合并.所以我猜这不是Capybara的错误.请参阅https://github.com/jnicklas/capybara/commit/755a724d4b10e6841a0eeb58af43375236b33247

MZa*_*oza 0

您可以执行保存前过滤器来清除该新行

在你的模型中添加

 before_save :clear_new_lines

 protected
 def clear_new_lines
   self.user_bio = user_bio.gsub("\n",'')
 end
Run Code Online (Sandbox Code Playgroud)