简体   繁体   English

RSpec 请求测试

[英]RSpec Request Tests

Slightly at my wits end here.有点不知所措到此为止。 Been trying to find a simple version of using request_spec.rb to write some basic controller tests in RSpec to better understand them, however so many are either complex, written in the controller_spec which i've read is now deprecated or built for purely a rails app not a Rails API.一直试图找到一个使用 request_spec.rb 的简单版本在 RSpec 中编写一些基本的控制器测试以更好地理解它们,但是很多都是复杂的,用我读过的 controller_spec 编写,现在已弃用或纯粹为 Rails 构建应用程序不是 Rails API。

 RSpec.describe "Users", type: :request do
  it "request list of all users" do
    user = User.create(name: "Ben", age: 20, favourite_mums_dish: "Spaghetti")
    get("/users")
    expect(response).to be_successful
    expect(response).to have_http_status(:success)
    expect(response.body).to include("Ben")
  end
end

The last does not work with an最后一个不适用于

     Failure/Error: expect(response.body).to include("Ben")
   expected "" to include "Ben"

How would I create a basic test to simply test that this user has been created and the response contains it?我将如何创建一个基本测试来简单地测试该用户是否已创建并且响应中包含它?

Thanks so much for your help in advance.非常感谢您的帮助。 Any documentation even would be helpful as I don't fully understand the Relish one either.任何文档甚至都会有所帮助,因为我也不完全理解 Relish 文档。

Ah thank you, I hadn't put the render in correctly!啊谢谢你,我没有正确放置渲染! Thanks so much for making sure my test was correct as I am new to that test I assumed I had written that wrong, not the controller!非常感谢您确保我的测试是正确的,因为我是该测试的新手,我认为我写错了,而不是控制器!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM