gpt4 book ai didi

ruby-on-rails - 带有 Rails Doorkeeper 的 MiniTest

转载 作者:行者123 更新时间:2023-12-04 06:25:20 27 4
gpt4 key购买 nike

Doorkeeper protected controllers in RSpec 的等价物是什么在 Minitest 中?

let(:token) { double :accessible? => true }

before do
controller.stub(:doorkeeper_token) { token }
end

我想用 Minitest 测试我的,目前所有的响应都是“Expected response to be a , but was <401>”

最佳答案

基于@riffraff 的回答:

gem 'mocha' 添加到您的 Gemfile 然后 bundle install ( https://github.com/freerange/mocha )

为您的测试添加一些助手:

# test/test_helper.rb
# ...
require "mocha/test_unit"

class ActiveSupport::TestCase
fixtures :all

# ...

def sign_in user
token = Doorkeeper::AccessToken.new(resource_owner_id: user.id)
ApplicationController.any_instance.stubs(:doorkeeper_token).returns(token)
end
end

在测试中使用你的新助手:

test 'Whatever you want' do
sign_in users(:one) # here is an example using fixtures
# Do your get / post / etc
# Do your assertions
end

关于ruby-on-rails - 带有 Rails Doorkeeper 的 MiniTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22387975/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com