Rspec控制器测试,传递JSON参数 [英] Rspec Controller tests, passing JSON params

查看:134
本文介绍了Rspec控制器测试,传递JSON参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以下目标:在RSpec控制器测试中创建POST json请求,并将其传递给参数.这是我的代码

I'm trying to achieve the following: create a POST json request within RSpec Controller test, passing it params. Here's my code

it 'returns access_token' do
    post :login, email: 'bla', password: 'bla1', format: :json
end

我在控制器request.body.read中得到的是一个带有参数的字符串,但是像这样传递 email=bla&password=bla1

What I get in controllers request.body.read is a string with params, but passed like this email=bla&password=bla1

这绝对不是JSON.但是,如果我使用CURL发出请求

This is definitely not a JSON. But, if I make request using CURL

curl -d '{"email": "bla@bla.com" }' http://127.0.0.1:3000/users/login --header "Accept: application/json" --header "Content-Type: application/json"

我将我的request.body.read读取为正确的json

I get my request.body.read as a correct json

"{\"email\": \"bla@bla.com\", \"password\": \"bla1\" }"

那么我该如何从我的rspec中传递它呢?

So how do I pass it this way from my rspec?

推荐答案

post "/models/#{@model.id}",
        params: {
            some_field: "1234",
        },
        as: :json

这篇关于Rspec控制器测试,传递JSON参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆