gpt4 book ai didi

ruby - 在 Padrino 上模拟 current_account 以进行 rspec 测试

转载 作者:数据小太阳 更新时间:2023-10-29 07:17:53 24 4
gpt4 key购买 nike

我正在尝试测试依赖于 Padrino::Admin::AccessControl 提供的 current_account 的 padrino Controller

为此,我需要模拟 current_account。

代码是这样的:

App.controller :post do
post :create, map => '/create' do
Post.create :user => current_account
end
end

和 rspec:

describe "Post creation" do
it 'should create' do
account = Account.create :name => 'someone'
loggin_as account #to mock current_account
post '/create'
Post.first.user.should == account
end
end

如何实现“loggin_as”或如何编写此测试?

最佳答案

我找到了一个简单的测试方法:

App.any_instance.stub(:current_account).and_return(account)

所以,测试代码应该是:

describe "Post creation" do
it 'should create' do
account = Account.create :name => 'someone'
App.any_instance.stub(:current_account).and_return(account)
post '/create'
Post.first.user.should == account
end
end

但我仍然喜欢构建“loggin_as”助手。那么,如何动态获取 App 类呢? (我应该为这个问题创建另一个线程吗?)

关于ruby - 在 Padrino 上模拟 current_account 以进行 rspec 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13498740/

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