[英]Failed testing a hash with rspec-rails
I'm building a simple API and one of the features is the fact it must inform the users its version. 我正在构建一个简单的API,其功能之一就是必须将其版本告知用户。 Then I have a Api controller with a version method 然后我有一个带有版本方法的Api控制器
class ApiController < ApplicationController
def version
render :json => {version: '0.0.1'}
end
end
and I also have a route in my config/routes.rb 而且我的config / routes.rb中也有一条路由
Rails.application.routes.draw do
root 'site#index'
get 'api/version'
end
I'm using Rspec and this is the my failing test 我正在使用Rspec,这是我失败的测试
it 'returns the correct version number (0.0.1 by now)' do
get :version
expect(response.body).to eq({'version':'0.0.1'})
end
It happens that my test fails ( http://imgur.com/7MPL8RO ) 碰巧我的测试失败( http://imgur.com/7MPL8RO )
1) ApiController GET #version returns the correct version number (0.0.1 by now)
Failure/Error: expect(response.body).to eq(a)
expected: {:version=>"0.0.1"}
got: "{\"version\":\"0.0.1\"}"
What is the point here? 这里有什么意义? Should I test against response.body.to_s or something like that? 我应该测试response.body.to_s还是类似的东西?
It seems I'm missing some point, probably an easy one. 似乎我遗漏了一点,可能很简单。
response.body
returns a serialized json string whereas {'version':'0.0.1'}
is a Hash
. response.body
返回一个序列化的json字符串,而{'version':'0.0.1'}
是一个Hash
。
Your expectation should be expect(response.body).to eq({'version':'0.0.1'}.to_json)
您的期望应该是expect(response.body).to eq({'version':'0.0.1'}.to_json)
or 要么
expect(JSON.parse(response.body)).to eq({'version':'0.0.1'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.