ruby-on-rails - rspec:失败/错误:_send_(方法,文件)。syntaxError:104:语法错误,意外的keyword_end,期待输入结束
问题描述
在终端窗口中执行 bundle exec rspec 之后。我收到两个错误:[失败/错误:发送(方法,文件)和语法错误:/vagrant/src/grammable/spec/controllers/grams_controller_spec.rb:104:语法错误,意外的keyword_end,期待输入结束] . 由于示例之外发生了 1 个错误,我无法通过测试。
require 'rails_helper'
RSpec.describe GramsController, type: :controller
describe "grams#update action" do
it "should allow users to successfully update grams" do
gram = FactoryBot.create(:gram, message: "Initial Value")
patch :update, params: { id: gram.id, gram: { message: 'Changed' } }
expect(response).to redirect_to root_path
gram.reload
expect(gram.message).to eq "Changed"
end
it "should have http 404 error if the gram cannot be found" do
patch :update, params: { id: "YOLOSWAG", gram: { message: 'Changed' } }
expect(response).to have_http_status(:not_found)
end
it "should render the edit form with an http status of unprocessable_entity" do
gram = FactoryBot.create(:gram, message: "Initial Value")
patch :update, params: { id: gram.id, gram: { message: '' } }
expect(response).to have_http_status(:unprocessable_entity)
gram.reload
expect(gram.message).to eq "Initial Value"
end
end
describe "grams#edit action" do
it "should successfully show the edit form if the gram is found" do
gram = FactoryBot.create(:gram)
get :edit, params: { id: gram.id }
expect(response).to have_http_status(:success)
end
it "should return a 404 error message if the gram is not found" do
get :edit, params: { id: 'SWAG' }
expect(response).to have_http_status(:not_found)
end
end
describe "grams#show action" do
it "should successfully show the page if the gram is found" do
gram = FactoryBot.create(:gram)
get :show, params: { id: gram.id }
expect(response).to have_http_status(:success)
end
it "should return a 404 error if the gram is not found" do
get :show, params: { id: 'TACOCAT' }
expect(response).to have_http_status(:not_found)
end
end
describe "grams#index action" do
it "should successfully show the page" do
get :index
expect(response).to have_http_status(:success)
end
end
describe "grams#new action" do
it "should require users to be logged in" do
get :new
expect(response).to redirect_to new_user_session_path
end
it "should successfully show the new form" do
user = FactoryBot.create(:user)
sign_in user
get :new
expect(response).to have_http_status(:success)
end
end
describe "grams#create action" do
it "should require users to be logged in" do
post :create, params: { gram: { message: "Hello" } }
expect(response).to redirect_to new_user_session_path
end
it "should successfully create a new gram in our database" do
user = FactoryBot.create(:user)
sign_in user
post :create, params: { gram: { message: 'Hello!' } }
expect(response).to redirect_to root_path
gram = Gram.last
expect(gram.message).to eq("Hello!")
expect(gram.user).to eq(user)
end
it "should properly deal with validation errors" do
user = FactoryBot.create(:user)
sign_in user
post :create, params: { gram: { message: '' } }
expect(response).to have_http_status(:unprocessable_entity)
expect(Gram.count).to eq Gram.count
end
end
end
解决方案
我认为你错过do
了第一行 RSpec.describe GramsController, type: :controller
。在编辑后的代码下方找到。希望这可以帮助!
require 'rails_helper'
RSpec.describe GramsController, type: :controller do
describe "grams#update action" do
it "should allow users to successfully update grams" do
gram = FactoryBot.create(:gram, message: "Initial Value")
patch :update, params: { id: gram.id, gram: { message: 'Changed' } }
expect(response).to redirect_to root_path
gram.reload
expect(gram.message).to eq "Changed"
end
it "should have http 404 error if the gram cannot be found" do
patch :update, params: { id: "YOLOSWAG", gram: { message: 'Changed' } }
expect(response).to have_http_status(:not_found)
end
it "should render the edit form with an http status of unprocessable_entity" do
gram = FactoryBot.create(:gram, message: "Initial Value")
patch :update, params: { id: gram.id, gram: { message: '' } }
expect(response).to have_http_status(:unprocessable_entity)
gram.reload
expect(gram.message).to eq "Initial Value"
end
end
describe "grams#edit action" do
it "should successfully show the edit form if the gram is found" do
gram = FactoryBot.create(:gram)
get :edit, params: { id: gram.id }
expect(response).to have_http_status(:success)
end
it "should return a 404 error message if the gram is not found" do
get :edit, params: { id: 'SWAG' }
expect(response).to have_http_status(:not_found)
end
end
describe "grams#show action" do
it "should successfully show the page if the gram is found" do
gram = FactoryBot.create(:gram)
get :show, params: { id: gram.id }
expect(response).to have_http_status(:success)
end
it "should return a 404 error if the gram is not found" do
get :show, params: { id: 'TACOCAT' }
expect(response).to have_http_status(:not_found)
end
end
describe "grams#index action" do
it "should successfully show the page" do
get :index
expect(response).to have_http_status(:success)
end
end
describe "grams#new action" do
it "should require users to be logged in" do
get :new
expect(response).to redirect_to new_user_session_path
end
it "should successfully show the new form" do
user = FactoryBot.create(:user)
sign_in user
get :new
expect(response).to have_http_status(:success)
end
end
describe "grams#create action" do
it "should require users to be logged in" do
post :create, params: { gram: { message: "Hello" } }
expect(response).to redirect_to new_user_session_path
end
it "should successfully create a new gram in our database" do
user = FactoryBot.create(:user)
sign_in user
post :create, params: { gram: { message: 'Hello!' } }
expect(response).to redirect_to root_path
gram = Gram.last
expect(gram.message).to eq("Hello!")
expect(gram.user).to eq(user)
end
it "should properly deal with validation errors" do
user = FactoryBot.create(:user)
sign_in user
post :create, params: { gram: { message: '' } }
expect(response).to have_http_status(:unprocessable_entity)
expect(Gram.count).to eq Gram.count
end
end
end
推荐阅读
- webserver - 兼容 H.264 且轻量级的 Android 版 Web 服务器
- java - 为什么要以 "" 开头的字符串(例如,String id = "" + method())?
- c++ - 如何在 C++ 程序中编写一个自动生成员工 ID 的函数?
- angular - ERESOLVE 无法解析依赖树?
- xml - 为什么使用 JUnitXmlReporter 在量角器中 XML 报告显示失败计数是双倍的
- go - 如何在 Geziyor 中使用 headless chrome / chromedp?
- android - 尽管 Firebase 已连接到 Android Studio,但该应用无法连接到 Firebase
- html - 使用 transform: rotateZ(180deg) translateY(2px) css 属性如何旋转下拉箭头?
- javascript - 如何根据值突出显示表格行
- ardalis-specification - 在违反 OOP 的核心层中使用 ISpecification