Rails如何修复格式错误的请求(错误代码400)?

伊吉

我正在为API运行RSpec,专门用于创建帖子(我也正在:update和:destroy上工作,但是那两个都运行良好。我在:create上遇到麻烦)

这是我的RSpec:

describe "POST create" do
   before { post :create, topic_id: my_topic.id, post: {title: @new_post.title, body: @new_post.body} }

   it "returns http success" do
     expect(response).to have_http_status(:success)
   end

   it "returns json content type" do
     expect(response.content_type).to eq 'application/json'
   end

   it "creates a topic with the correct attributes" do
     hashed_json = JSON.parse(response.body)
     expect(hashed_json["title"]).to eq(@new_post.title)
     expect(hashed_json["body"]).to eq(@new_post.body)
   end
 end

这是我的创作

def create
    post = Post.new(post_params)

    if post.valid?
      post.save!
      render json: post.to_json, status: 201
    else
      render json: {error: "Post is invalid", status: 400}, status: 400
    end
  end

这是我不断得到的错误代码:

.........F.F....

Failures:

  1) Api::V1::PostsController authenticated and authorized users POST create returns http success
     Failure/Error: expect(response).to have_http_status(:success)
       expected the response to have a success status code (2xx) but it was 400
     # ./spec/api/v1/controllers/posts_controller_spec.rb:78:in `block (4 levels) in <top (required)>'

我真的不确定代码有什么问题。路线工作正常。如何使测试通过?

托马斯·科尔

为了更好地解决THA @spickermann建议,改变post@post你的#create行动,并从那里下面添加到您的规格和工作的代码。我敢打赌,您必须@post.user = current_user在控制器中执行类似的操作。

it "@post is valid and have no errors" do
  expect(assigns[:post]).to be_valid
  expect(assigns[:post].errors).to be_empty
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python Flask请求400错误代码

来自分类Dev

如何修复 HTTP 错误 400:错误请求?

来自分类Dev

如何在MySQL中修复错误代码1241

来自分类Dev

如何修复MySQL 8错误代码:1525和1292?

来自分类Dev

如何修复错误代码CS1503?

来自分类Dev

如何快速修复 JSON-RPC 错误代码 591

来自分类Dev

Boost Asio https同步呼叫-错误代码400错误的请求

来自分类Dev

Watson视觉识别错误代码400

来自分类Dev

Parse.com REST API错误代码400:从Java HTTP请求到云功能的错误请求

来自分类Dev

FCM 发送 HTTP Post 请求给出错误代码 400()

来自分类Dev

如何修复错误E:子进程/ usr / bin / dpkg返回了错误代码(1)

来自分类Dev

错误的错误代码

来自分类Dev

Rails:JSON中的验证错误代码

来自分类Dev

PHPMYADMIN ,,处理请求错误代码中的错误代码:200错误文本:OK

来自分类Dev

如何修复错误代码1241:在MySQL查询中,操作数应包含1列?

来自分类Dev

如何修复E:子进程/ usr / bin / dpkg返回错误代码(2)?

来自分类Dev

如何在Visual Studio 2015中修复错误代码CS0433

来自分类Dev

如何在 Java 中修复 mediawiki-api 上的“错误代码=mustpostparams”

来自分类Dev

当RequestBody参数的某些属性为null时,如何返回400 HTTP错误代码?

来自分类Dev

当RequestBody参数的某些属性为null时,如何返回400 HTTP错误代码?

来自分类Dev

如何在春季JPA应用程序中集成Paypal Rest API。错误代码400

来自分类Dev

Typescript错误代码及其修复的完整列表

来自分类Dev

gmail发送api流星失败,错误代码为400

来自分类Dev

as400 / IBM I上的pthread错误代码3029?

来自分类Dev

Android HttpURLConnection POST RequestMethod 导致 400 错误代码

来自分类Dev

Spotify API 错误代码 400 - 无搜索查询

来自分类Dev

在 Volley 中实现 POST 请求时出现错误代码 400,但在使用 OkHttp 时则不会

来自分类Dev

如何处理CORS错误代码?

来自分类Dev

如何从Qt获取sqlite错误代码

Related 相关文章

  1. 1

    Python Flask请求400错误代码

  2. 2

    如何修复 HTTP 错误 400:错误请求?

  3. 3

    如何在MySQL中修复错误代码1241

  4. 4

    如何修复MySQL 8错误代码:1525和1292?

  5. 5

    如何修复错误代码CS1503?

  6. 6

    如何快速修复 JSON-RPC 错误代码 591

  7. 7

    Boost Asio https同步呼叫-错误代码400错误的请求

  8. 8

    Watson视觉识别错误代码400

  9. 9

    Parse.com REST API错误代码400:从Java HTTP请求到云功能的错误请求

  10. 10

    FCM 发送 HTTP Post 请求给出错误代码 400()

  11. 11

    如何修复错误E:子进程/ usr / bin / dpkg返回了错误代码(1)

  12. 12

    错误的错误代码

  13. 13

    Rails:JSON中的验证错误代码

  14. 14

    PHPMYADMIN ,,处理请求错误代码中的错误代码:200错误文本:OK

  15. 15

    如何修复错误代码1241:在MySQL查询中,操作数应包含1列?

  16. 16

    如何修复E:子进程/ usr / bin / dpkg返回错误代码(2)?

  17. 17

    如何在Visual Studio 2015中修复错误代码CS0433

  18. 18

    如何在 Java 中修复 mediawiki-api 上的“错误代码=mustpostparams”

  19. 19

    当RequestBody参数的某些属性为null时,如何返回400 HTTP错误代码?

  20. 20

    当RequestBody参数的某些属性为null时,如何返回400 HTTP错误代码?

  21. 21

    如何在春季JPA应用程序中集成Paypal Rest API。错误代码400

  22. 22

    Typescript错误代码及其修复的完整列表

  23. 23

    gmail发送api流星失败,错误代码为400

  24. 24

    as400 / IBM I上的pthread错误代码3029?

  25. 25

    Android HttpURLConnection POST RequestMethod 导致 400 错误代码

  26. 26

    Spotify API 错误代码 400 - 无搜索查询

  27. 27

    在 Volley 中实现 POST 请求时出现错误代码 400,但在使用 OkHttp 时则不会

  28. 28

    如何处理CORS错误代码?

  29. 29

    如何从Qt获取sqlite错误代码

热门标签

归档