首页 > 解决方案 > 这是我的用户控制器。如何在 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

标签: ruby-on-railsruby-on-rails-3ruby-on-rails-4

解决方案


如果您使用 Rspec gem 编写测试用例,那么您可以使用教程点 rspec Turorials。它将提供良好的开端,让您了解测试在 Ruby on Rails 中的工作原理。您还可以尝试一些电子书以获取深入的知识。


推荐阅读