[英]Rails RSpec: Test the "show" page
I am struggling with a very simple issue - as I am trying to learn RSpec:我正在努力解决一个非常简单的问题 - 因为我正在尝试学习 RSpec:
I want to ensure that when I go to the "show" page of my model and pass in the ID of a given record, the response is a success.我想确保当我转到模型的“显示”页面并传入给定记录的 ID 时,响应是成功的。 I am trying to adopt a bit of TDD, so haven't even customized the view (although there is an empty show.html.erb available).我正在尝试采用一些 TDD,所以甚至没有自定义视图(尽管有一个空的 show.html.erb 可用)。
Here's the test:这是测试:
require 'rails_helper'
RSpec.describe Book, type: :request do
describe 'GET /show' do
it 'returns http success' do
book = Book.create(title: 'The Hobbit', year: 1937)
get :show, params: { id: book.to_param }
expect(response).to be_success
end
end
end
I don't get how I need to write this.我不明白我需要如何写这个。 I get an error like this:我收到这样的错误:
Failure/Error: get :show, params: { id: book.to_param }
URI::InvalidURIError:
bad URI(is not URI?): "http://www.example.com:80show"
Any hints?有什么提示吗?
Replace your test with this and run it:用这个替换你的测试并运行它:
require "rails_helper"
RSpec.feature "Users can view books >" do
scenario "by clicking the Show link" do
book = Book.create(title: "The Hobbit", year: 1937)
visit "/"
click_link "Show"
expect(page).to have_content "The Hobbit"
end
end
I found out that I needed to describe the spec differently.我发现我需要以不同的方式描述规范。 This is what worked - found here .这就是在这里找到的。
require 'rails_helper'
RSpec.describe BooksController, type: :controller do
describe 'GET /show' do
it 'returns http success' do
book = Book.create(title: 'The Hobbit', year: 1937)
get :show, params: { id: book.to_param }
expect(response).to have_http_status(200)
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.