before_action 回调不执行,而 运行 集成测试用例 rails
The before_action call back do not execute while running the integration test cases rails
使用 ruby 2.6.5,Rails 6.0.3.7
before_action 过滤器在 运行 开发服务器中的项目时工作正常。但是 运行 rails 应用程序的集成测试。
回调不执行,请求直接转到被调用的函数,而不是先转到之前的操作。
这里附上我的控制器和集成测试用例以及错误输出。
控制器
class TvSeriesController < ApplicationController
before_action :check_file, only: [:create, :tv_seriel_comments]
require 'roo'
def index
@tv_series = TvSeriel.includes(:comments).all
end
def show
@series = TvSeriel.includes({ comments: [:user] }).find(params[:id])
end
def create
spreadsheet = Roo::Spreadsheet.open(params[:file])
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |line|
row = HashWithIndifferentAccess[[header, spreadsheet.row(line)].transpose]
tv_serial = TvSeriel.new
tv_serial.name = row['TV Series']
tv_serial.genre = row["Genre"]
tv_serial.seasons = row["No of seasons"]
tv_serial.release_date = row["Date of First Release"]
tv_serial.director = row["Director"]
tv_serial.actor = row["Actor"]
tv_serial.shoot_location = row["Shoot location"]
tv_serial.country = row["Country"]
tv_serial.save
end
flash[:notice] = 'TV series uploaded!'
redirect_back(fallback_location: tv_series_index_path)
end
def tv_seriel_comments
spreadsheet = Roo::Spreadsheet.open(params[:file])
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |line|
row = HashWithIndifferentAccess[[header, spreadsheet.row(line)].transpose]
comment = Comment.new
user = User.find_by_name(row["User"])
comment.user = user
tv_serial = TvSeriel.find_by_name(row['TV Series'])
comment.tv_seriel = tv_serial
comment.stars = row['Stars']
comment.review = row['Review']
comment.save
end
flash[:notice] = 'TV series comment uploaded!'
redirect_back(fallback_location: tv_series_index_path)
end
def destroy
TvSeriel.find(params[:id]).destroy
flash[:notice] = 'TV series removed success!'
redirect_back(fallback_location: tv_series_index_path)
end
def search_actor
@tv_series = TvSeriel.includes(:comments).where("actor like ?", "%#{params[:actor]}%")
render 'tv_series/index'
end
## check if uploaded file is a cvs
def check_file
unless params[:file].blank?
if params[:file].content_type.eql?("text/csv")
else
flash[:alert] = "file format Invalid, expected: text/csv Got #{params[:file].content_type}"
redirect_back(fallback_location: :tv_series_index_path)
end
else
flash[:alert] = 'Please provide a file to be uploaded !'
end
end
end
集成测试:
require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
fixtures :all
include Devise::Test::IntegrationHelpers
include Warden::Test::Helpers
test 'Get list of all users' do
@user = users(:vidur)
sign_in(@user)
get "/users"
assert_response :success
assert_select "h2", "User List"
end
test 'Update Series' do
@user = users(:vidur)
sign_in(@user)
post '/tv_series'
assert_response :success
end
end
错误:
# Running:
E
Error:
UserFlowsTest#test_Update_Series:
TypeError: no implicit conversion of nil into String
app/controllers/tv_series_controller.rb:15:in `create'
test/integration/user_flows_test.rb:19:in `block in <class:UserFlowsTest>'
此处 before_action 过滤器不执行,请求直接转到定义的操作,任何人都可以给出仅 运行 测试的原因,以及如何更正它,在此先感谢。
这是完整代码的 git 存储库:https://github.com/vidurpunj/sopra_test.
那是因为你没有file
参数。
所以你的check_file
正在使用这部分:
flash[:alert] = 'Please provide a file to be uploaded !'
并且由于您不呈现或重定向,控制器操作将正常调用。有关详细信息,请参阅 Rails documentation on filters:
If a "before" filter renders or redirects, the action will not run
使用 ruby 2.6.5,Rails 6.0.3.7
before_action 过滤器在 运行 开发服务器中的项目时工作正常。但是 运行 rails 应用程序的集成测试。 回调不执行,请求直接转到被调用的函数,而不是先转到之前的操作。
这里附上我的控制器和集成测试用例以及错误输出。 控制器
class TvSeriesController < ApplicationController
before_action :check_file, only: [:create, :tv_seriel_comments]
require 'roo'
def index
@tv_series = TvSeriel.includes(:comments).all
end
def show
@series = TvSeriel.includes({ comments: [:user] }).find(params[:id])
end
def create
spreadsheet = Roo::Spreadsheet.open(params[:file])
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |line|
row = HashWithIndifferentAccess[[header, spreadsheet.row(line)].transpose]
tv_serial = TvSeriel.new
tv_serial.name = row['TV Series']
tv_serial.genre = row["Genre"]
tv_serial.seasons = row["No of seasons"]
tv_serial.release_date = row["Date of First Release"]
tv_serial.director = row["Director"]
tv_serial.actor = row["Actor"]
tv_serial.shoot_location = row["Shoot location"]
tv_serial.country = row["Country"]
tv_serial.save
end
flash[:notice] = 'TV series uploaded!'
redirect_back(fallback_location: tv_series_index_path)
end
def tv_seriel_comments
spreadsheet = Roo::Spreadsheet.open(params[:file])
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |line|
row = HashWithIndifferentAccess[[header, spreadsheet.row(line)].transpose]
comment = Comment.new
user = User.find_by_name(row["User"])
comment.user = user
tv_serial = TvSeriel.find_by_name(row['TV Series'])
comment.tv_seriel = tv_serial
comment.stars = row['Stars']
comment.review = row['Review']
comment.save
end
flash[:notice] = 'TV series comment uploaded!'
redirect_back(fallback_location: tv_series_index_path)
end
def destroy
TvSeriel.find(params[:id]).destroy
flash[:notice] = 'TV series removed success!'
redirect_back(fallback_location: tv_series_index_path)
end
def search_actor
@tv_series = TvSeriel.includes(:comments).where("actor like ?", "%#{params[:actor]}%")
render 'tv_series/index'
end
## check if uploaded file is a cvs
def check_file
unless params[:file].blank?
if params[:file].content_type.eql?("text/csv")
else
flash[:alert] = "file format Invalid, expected: text/csv Got #{params[:file].content_type}"
redirect_back(fallback_location: :tv_series_index_path)
end
else
flash[:alert] = 'Please provide a file to be uploaded !'
end
end
end
集成测试:
require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
fixtures :all
include Devise::Test::IntegrationHelpers
include Warden::Test::Helpers
test 'Get list of all users' do
@user = users(:vidur)
sign_in(@user)
get "/users"
assert_response :success
assert_select "h2", "User List"
end
test 'Update Series' do
@user = users(:vidur)
sign_in(@user)
post '/tv_series'
assert_response :success
end
end
错误:
# Running:
E
Error:
UserFlowsTest#test_Update_Series:
TypeError: no implicit conversion of nil into String
app/controllers/tv_series_controller.rb:15:in `create'
test/integration/user_flows_test.rb:19:in `block in <class:UserFlowsTest>'
此处 before_action 过滤器不执行,请求直接转到定义的操作,任何人都可以给出仅 运行 测试的原因,以及如何更正它,在此先感谢。 这是完整代码的 git 存储库:https://github.com/vidurpunj/sopra_test.
那是因为你没有file
参数。
所以你的check_file
正在使用这部分:
flash[:alert] = 'Please provide a file to be uploaded !'
并且由于您不呈现或重定向,控制器操作将正常调用。有关详细信息,请参阅 Rails documentation on filters:
If a "before" filter renders or redirects, the action will not run