我该如何让webrat测试textarea的内容

时间:2012-11-23 05:46:27

标签: ruby-on-rails capybara webrat

我正在尝试使用正确的语法让webrat检查表单中textarea的内容。

我可以用

检查textarea的存在
response.should have_selector('textarea')

contains方法似乎只检查响应中的可见文本。有用于检查textarea内容的水豚方法,但是我正在为Capybara制作大量现有的规格,看起来都是在WebRat中,当我尝试使用像are_field这样的Capybara方法时,我会继续得到以下内容错误:

RSpec::Expectations::ExpectationNotMetError Exception: expected field "textarea" to return something

我找到了以下内容的示例:

response.should have_selector('textarea', :content => 'blah')

但奇怪的是,这会导致调试器继续运行,但我还没有能够有效地测试它作为解决方案。

关于正确方法的任何提示或建议都非常赞赏

这是我在调试器中测试的堆栈跟踪

(rdb:1) p response.should have_selector('textarea#article_body_and_extended', :content =>  text)
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/interface.rb:70:in `printf'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/interface.rb:70:in `print'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/processor.rb:375:in `print'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/command.rb:165:in `print'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/command.rb:183:in `rescue in debug_eval'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/command.rb:173:in `debug_eval'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/commands/eval.rb:47:in `block in execute'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/commands/eval.rb:19:in `run_with_binding'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/commands/eval.rb:46:in `execute'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/processor.rb:275:in `one_cmd'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/processor.rb:261:in `block (2 levels) in process_commands'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/processor.rb:260:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/processor.rb:260:in `block in process_commands'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/processor.rb:253:in `catch'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/processor.rb:253:in `process_commands'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug19-0.11.6/cli/ruby-debug/processor.rb:173:in `at_line'
(eval):5:in `block in at_line'
<internal:prelude>:10:in `synchronize'
(eval):3:in `at_line'
/usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug-base19-0.11.25/lib/ruby-debug-base.rb:55:in `at_line'
/home/ubuntu/Documents/hw5/typo/spec/controllers/admin/content_controller_spec.rb:509:in `block (4 levels) in <top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:113:in `instance_eval'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:113:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:253:in `with_around_each_hooks'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:110:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:378:in `block in run_examples'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:374:in `map'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:374:in `run_examples'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:360:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `map'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `map'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `map'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/reporter.rb:34:in `report'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:25:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'F

然后调试器继续并且检查失败:

    text = @article.body + @article_for_merging.body + "\n&lt;<!--more-->&gt;\n" +@article.extended
    debugger
    response.should have_selector('textarea#article_body_and_extended', :content =>  text)

即使当我对响应正文进行正则表达式时,我也会得到一个匹配

(rdb:1) p text
"A content with several datasomething we will merge&lt;<!--more-->&gt;extended content for fun"
(rdb:1) p response.body =~ text
TypeError Exception: type mismatch: String given
(rdb:1) p response.body =~ /text/
218

我真正喜欢的只是将textarea的实际内容拉出来的方式......虽然我觉得我应该写一个模型规范而不是控制器规范...

1 个答案:

答案 0 :(得分:0)

所以我有点放弃让这个工作。我想我跟踪了正确的语法:

response.should have_selector('textarea', :name => "article[body_and_extended]",:content =>  text)

我将文本设置为预期内容。我花了一个小时检查我确切地知道textarea中的文本,但无法使其工作 - 可能是编码问题或其他问题。但是这有什么用呢:

    text = @article.body + @article_for_merging.body + "\n&lt;!--more--&gt;\n" +@article.extended+@article_for_merging.extended
    position = response.body =~ /#{Regexp.quote(text)}/
    position.should_not be_nil

所以我很想继续前进,然后再回到这里......