Fiz*_*izz 142 json rspec ruby-on-rails
我的控制器中有以下代码:
format.json { render :json => {
:flashcard => @flashcard,
:lesson => @lesson,
:success => true
}
Run Code Online (Sandbox Code Playgroud)
在我的RSpec控制器测试中,我想验证某个场景确实收到了成功的json响应,所以我有以下行:
controller.should_receive(:render).with(hash_including(:success => true))
Run Code Online (Sandbox Code Playgroud)
虽然当我运行我的测试时,我收到以下错误:
Failure/Error: controller.should_receive(:render).with(hash_including(:success => false))
(#<AnnoController:0x00000002de0560>).render(hash_including(:success=>false))
expected: 1 time
received: 0 times
Run Code Online (Sandbox Code Playgroud)
我是否错误地检查了回复?
zet*_*tic 162
您可以检查响应对象并验证它是否包含预期值:
@expected = {
:flashcard => @flashcard,
:lesson => @lesson,
:success => true
}.to_json
get :action # replace with action name / params as necessary
response.body.should == @expected
Run Code Online (Sandbox Code Playgroud)
编辑
将其更改为a post会使其变得有点棘手.这是一种处理它的方法:
it "responds with JSON" do
my_model = stub_model(MyModel,:save=>true)
MyModel.stub(:new).with({'these' => 'params'}) { my_model }
post :create, :my_model => {'these' => 'params'}, :format => :json
response.body.should == my_model.to_json
end
Run Code Online (Sandbox Code Playgroud)
请注意,mock_model不会响应to_json,因此需要一个stub_model或一个真实的模型实例.
bre*_*c79 158
您可以像这样解析响应主体:
parsed_body = JSON.parse(response.body)
Run Code Online (Sandbox Code Playgroud)
然后,您可以针对已解析的内容进行断言.
parsed_body["foo"].should == "bar"
Run Code Online (Sandbox Code Playgroud)
lig*_*yrs 45
response.header['Content-Type'].should include 'application/json'
Run Code Online (Sandbox Code Playgroud)
Chi*_*iya 12
简单易行的方法.
# set some variable on success like :success => true in your controller
controller.rb
render :json => {:success => true, :data => data} # on success
spec_controller.rb
parse_json = JSON(response.body)
parse_json["success"].should == true
Run Code Online (Sandbox Code Playgroud)
Lor*_*lor 10
您也可以在里面定义一个辅助函数 spec/support/
module ApiHelpers
def json_body
JSON.parse(response.body)
end
end
RSpec.configure do |config|
config.include ApiHelpers, type: :request
end
Run Code Online (Sandbox Code Playgroud)
并json_body在需要访问JSON响应时使用.
例如,在您的请求规范中,您可以直接使用它
context 'when the request contains an authentication header' do
it 'should return the user info' do
user = create(:user)
get URL, headers: authenticated_header(user)
expect(response).to have_http_status(:ok)
expect(response.content_type).to eq('application/vnd.api+json')
expect(json_body["data"]["attributes"]["email"]).to eq(user.email)
expect(json_body["data"]["attributes"]["name"]).to eq(user.name)
end
end
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以查看'Content-Type'标题以查看它是否正确?
response.header['Content-Type'].should include 'text/javascript'
Run Code Online (Sandbox Code Playgroud)
另一种测试JSON响应的方法(不是内容中的内容包含预期值)是使用ActiveSupport解析响应:
ActiveSupport::JSON.decode(response.body).should_not be_nil
Run Code Online (Sandbox Code Playgroud)
如果响应不可解析JSON,则抛出异常并且测试将失败.
使用Rails 5(当前仍处于beta)时,parsed_body在测试响应上有一个新方法,该方法将返回解析为最后一个请求编码的响应。
在GitHub上的提交:https : //github.com/rails/rails/commit/eee3534b
| 归档时间: |
|
| 查看次数: |
144333 次 |
| 最近记录: |