我应该如何使用JBuilder视图测试控制器?

发布于 2025-01-23 22:04:03 字数 381 浏览 1 评论 0原文

这是我的控制器

class NewsController < ApplicationController 
    def show 
        @news = News.find_by id: params[:id]
    end
 end

,这是查看

json.extract! @news ,:id, :title , :content , :created_at , :updated_at
json.image @news.images do |image| 
    json.url url_for(image)
  end

测试该控制器和视图的正确方法。我是新手进行测试,所以我对此有点沮丧

this is my controller

class NewsController < ApplicationController 
    def show 
        @news = News.find_by id: params[:id]
    end
 end

this is views

json.extract! @news ,:id, :title , :content , :created_at , :updated_at
json.image @news.images do |image| 
    json.url url_for(image)
  end

what is right way of testing this controller and views. I'am new into testing so i'am little bit frustrated with this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2025-01-30 22:04:03

您可以验证状态和JSON结果

it "should have a 200 code" do
  get :show, params: { id: 1 }, format: :json
  expect(response.status).to eq 200
  expect(response.body).to eq expected_json
end

。 nofollow noreferrer“>此有关更多信息的文章

You can verify the status and the json result you are expecting

it "should have a 200 code" do
  get :show, params: { id: 1 }, format: :json
  expect(response.status).to eq 200
  expect(response.body).to eq expected_json
end

Refer this article for more info

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文