response.should have_text 导致未定义的方法“has_text?”

发布于 2024-09-25 19:46:54 字数 1142 浏览 2 评论 0原文

我想测试一个直接渲染一些 JSON 输出的控制器(通过使用“render :json => @entity_names”)。对于该任务,我在规范文件“response.should have_text('["enim", "enita"]')”中尝试过。不幸的是我总是收到这个错误: 失败/错误:response.should have_text('["enim", "enita"]') 未定义的方法“has_text?” for #

我是否错过了一些提供该方法的 gem?这是我的 Gemfile:

source 'http://rubygems.org'

gem 'rails', '>= 3.0.0'
gem 'mysql2'
gem 'mongrel'
gem 'devise'
gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git', :branch =>    'rails3'
gem 'thinking-sphinx', :git     => 'git://github.com/freelancing-god/thinking-sphinx.git', :branch  => 'rails3', :require => 'thinking_sphinx'

group :test, :development do
  gem 'rspec-rails', '>= 2.0.0.beta.19'
  gem 'steak', :git => 'git://github.com/cavalle/steak.git'
  gem 'webrat'
  gem 'capybara'
  gem 'capybara-envjs'
  gem 'shoulda'
  gem 'launchy'
  gem 'autotest'
  gem 'autotest-rails'
  gem 'test_notifier'
  gem 'rails3-generators'
  gem 'factory_girl_rails'
  gem 'populator'
  gem 'faker'
  gem 'random_data'
  gem 'database_cleaner', :git => 'git://github.com/bmabey/database_cleaner.git'
  gem 'delorean'
end

I would like to test a controller that directly renders some JSON output (by using "render :json => @entity_names"). For that task I tried in my spec file "response.should have_text('["enim", "enita"]')". Unfortunately I always get that error:
Failure/Error: response.should have_text('["enim", "enita"]')
undefined method `has_text?' for #

Do I miss some gem that provides that method? Here my Gemfile:

source 'http://rubygems.org'

gem 'rails', '>= 3.0.0'
gem 'mysql2'
gem 'mongrel'
gem 'devise'
gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git', :branch =>    'rails3'
gem 'thinking-sphinx', :git     => 'git://github.com/freelancing-god/thinking-sphinx.git', :branch  => 'rails3', :require => 'thinking_sphinx'

group :test, :development do
  gem 'rspec-rails', '>= 2.0.0.beta.19'
  gem 'steak', :git => 'git://github.com/cavalle/steak.git'
  gem 'webrat'
  gem 'capybara'
  gem 'capybara-envjs'
  gem 'shoulda'
  gem 'launchy'
  gem 'autotest'
  gem 'autotest-rails'
  gem 'test_notifier'
  gem 'rails3-generators'
  gem 'factory_girl_rails'
  gem 'populator'
  gem 'faker'
  gem 'random_data'
  gem 'database_cleaner', :git => 'git://github.com/bmabey/database_cleaner.git'
  gem 'delorean'
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-10-02 19:46:54

您可以将预期的输出构造为 JSON,然后获取响应正文(也是 JSON),对两者进行解码并进行比较。像这样的东西:

it "should do something" do
  expected = { :some_key => "and some value" }.to_json 
  xhr :post, :create, { :foo => "bar" }
  ActiveSupport::JSON.decode(response.body).should == ActiveSupport::JSON.decode(expected)
end 

You could construct your expected output as JSON, then get the response body (which is also JSON), decode both, and compare them. Something like:

it "should do something" do
  expected = { :some_key => "and some value" }.to_json 
  xhr :post, :create, { :foo => "bar" }
  ActiveSupport::JSON.decode(response.body).should == ActiveSupport::JSON.decode(expected)
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文