我正在努力解决一个非常简单的问题-因为我正在尝试学习RSpec:
我想确保当我进入"show"并传入给定记录的ID,则响应成功。我正在尝试采用一点TDD,所以甚至没有定制视图(尽管有一个空的show.html.erb可用)。
下面是测试:
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
我不明白我怎么需要写这个。我得到这样的错误:
Failure/Error: get :show, params: { id: book.to_param }
URI::InvalidURIError:
bad URI(is not URI?): "http://www.example.com:80show"
提示吗?
用这个替换您的测试并运行它:
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
我发现我需要用不同的方式来描述规范。
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