ruby-on-rails - 这是我的用户控制器。如何在 RSpec 中测试这段代码?
问题描述
如何在此用户登录中编写测试用例。我是 Rails 测试的新手,我正在使用 Rspec gem。我需要任何有关 Rails 测试的教程。
if params.key?(:mobile) && params.key?(:password)
user = User.find_by(mobile: params[:mobile], is_active: 1)
if user.nil?
output = { status: '06', message: 'Invalid user.' }.to_json
elsif user.authenticate(params[:password])
auth_token = JsonWebToken.encode(id: user.id,
user_type: user.user_type)
object = { id: user.id, user_type: user.user_type,
name: user.name }
output = { status: '10', message: 'Successful.',
auth_token: auth_token, user: object}.to_json
else
output = { status: '06', message: 'Incorrect password.' }.to_json
end
else
output = { status: '01', message: 'Invalid parameters.' }.to_json
end
render json: output, status: :ok
解决方案
如果您使用 Rspec gem 编写测试用例,那么您可以使用教程点 rspec Turorials。它将提供良好的开端,让您了解测试在 Ruby on Rails 中的工作原理。您还可以尝试一些电子书以获取深入的知识。
推荐阅读
- vuejs2 - Vue Google Place Autocomplete-如何使选定的值显示?
- spring - Spring Boot 错误 401 未经授权的模板
- c# - 如何在 C# 上执行 2 个不同的命令 cmd
- azure - 实现相同 Azure IR 的数据流的奇怪运行时
- macos - 在 Mac 上使用 dotnet core/vs code web api 时使用 Kestrel 的自签名证书错误
- sql - 无论如何要缩短我的 BigQuery 脚本(for 循环?)
- c++ - 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出错。这里的“虚拟”一词有什么意义?
- r - 当 R Shiny App 在 Linux 服务器上运行时下载文件
- visual-studio-code - 为什么 prettier 在我的配置中只忽略分号规则?
- python - 网页抓取 - 标签中的文本无法实现