Ahm*_*mza 5 ruby ruby-on-rails minitest devise rails-api
我使用的是devise_auth_token创业板rails-api应用程序,其中我试图测试,如果user是logged_in则user有权render通过index在行动welcome_controller。
欢迎控制器
class Api::WelcomeController < ApplicationController
before_action :authenticate_user!
def index
render json: {
data: {
message: "Welcome #{current_user.first_name}",
user: current_user
}
}, status: 200
end
end
Run Code Online (Sandbox Code Playgroud)
路线
Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'auth'
namespace :api do
get 'welcome', to: 'welcome#index'
resources :socities
end
end
Run Code Online (Sandbox Code Playgroud)
Welcome_controller_test
require 'test_helper'
class Api::WelcomeControllerTest < ActionDispatch::IntegrationTest
def test_index_renders_message
admin = users :admin
sign_in admin
get "/api/welcome"
assert_response :success
end
end
Run Code Online (Sandbox Code Playgroud)
夹具用户.yml
admin:
uid: admin@example.com
provider: email
email: admin@example.com
first_name: Ahmad
last_name: Hamza
nickname: nil
society_id: 1
encrypted_password: $2a$10$kPXgGIFeHxoek/UgfwtVFeLCLYgIB82Cazj8c6GZz2I/p68ohfp9K #1234test
confirmed_at: 2017-01-02 08:31:23
confirmation_sent_at: 2017-01-02 08:30:59
role: admin
Run Code Online (Sandbox Code Playgroud)
test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
class ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
end
Run Code Online (Sandbox Code Playgroud)
错误
Failure:
Api::WelcomeControllerTest#test_index_renders_message [/Users/ahmadhamza/sites/manage_society/test/controllers/api/welcome_controller_test.rb:8]:
Expected response to be a <2XX: success>, but was a <401: Unauthorized>
Run Code Online (Sandbox Code Playgroud)
如何检查发生了什么问题?
| 归档时间: |
|
| 查看次数: |
748 次 |
| 最近记录: |