response.should have_text导致未定义的方法`has_text?'

时间:2010-10-01 05:28:50

标签: ruby-on-rails ruby-on-rails-3 rspec rspec2

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

我是否想念一些提供该方法的宝石?这是我的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

1 个答案:

答案 0 :(得分:1)

您可以将预期输出构造为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