我正在尝试为我的gem编写规范,它生成otp并将其保存在db中.现在我正在为它编写规范.所以基本上我有三个方法generate_otp!,regenerate_otp!,verify_otp(otp).是什么generate_otp!做的是它调用的方法generate_otp包含三个变量
otp_code - 基本上是使用secure_random生成的随机值otp_verified - 一个布尔值,用于设置是否验证otp的状态otp_expiry_time - 设置otp的到期时间,可由rails app在配置中设置.这三个都是我的数据库的列.
现在generate_otp我调用了将值保存到db active_records的save方法之后.
现在进行测试我正在使用:memory:并在表中存储值.经过测试我正在丢弃数据库.现在,我没有得到如何存根随机生成值和测试值是否被分配或不全部三列,即otp_code,otp_verified,otp_expiry_time.
这是我需要测试的方法的代码.
def generate_otp
self.otp_verified = false
self.otp_expiry_time = OtpGenerator::configuration.otp_expiry_time
self.otp_code = SecureRandom.hex(4)
end
def generate_otp!
generate_otp
save
end
Run Code Online (Sandbox Code Playgroud)
对此有何帮助?我也检查了这个问题,但没有多大帮助.这是我第一次编写规范,并且对rspecs没有那么多经验.我还研究了mock和stub 的官方文档,但我真的很困惑.
更新:otp_spec代码
require 'spec_helper'
describe OtpGenerator do
let(:user) { build_mock_class.new() }
before(:all) { create_table }
after(:all) { drop_table } …Run Code Online (Sandbox Code Playgroud) 从过去几周开始,我看到此错误“无法在 5.000 秒内获取数据库连接”。我尝试过以下解决方案。
根据 Sidekiq文档,我已将并发数减少到 10,并将 MySql 数据库池大小增加到 12。但我仍然收到此错误,因此我将数据库池大小增加到 27。但问题仍然存在。尝试了这个和这个答案的解决方案。我的 Sidekiq 服务器计算机没有运行任何其他可以消耗 AR 连接的进程。我不会从外部旋转任何线程。以下是我的配置
Sidekiq version - 3.4.2
Rails version - 4.2.4
Active Record version - 4.2.4
Database pool size - 27
Sidekiq concurrency - 10
Run Code Online (Sandbox Code Playgroud)
对此的任何帮助将不胜感激。
我正在为我的第一颗宝石编写规格。但我遇到了这个奇怪的错误。我的 rspec 的代码是
describe '#success' do
let(:resp) { {"TwilioResponse"=>{"SMSMessage"=>{"Sid"=>"0d1c0cbfb2b5e8f97dddb4479bdbbc6a", "AccountSid"=>"exotel_sid", "From"=>"/exotel_sid", "To"=>"1234", "DateCreated"=>"2016-07-18 15:35:29", "DateUpdated"=>"2016-07-18 15:35:29", "DateSent"=>nil, "Body"=>"test sms", "Direction"=>"outbound-api", "Uri"=>"/v1/Accounts/exotel_sid/Sms/Messages/0d1c0cbfb2b5e8f97dddb4479bdbbc6a", "ApiVersion"=>nil, "Price"=>nil, "Status"=>"queued"}}} }
before{ allow(Generator::Exotel).to receive(:post).with("/#{Generator::configuration.sid}/Sms/send",
{:body => {:To => 1234, :Body => 'test sms'},:basic_auth=>{:username=>"#{Generator::configuration.sid}", :password=>"#{Generator::configuration.token}"}}).and_return(resp) }
it 'returns response object' do
response = Generator::Exotel.send(:to => 1234, :body => "test sms")
expect(response).to eq ({"Status"=>200, "Message"=>"Success"})
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行时rspec出现此错误
NoMethodError:
undefined method `code' for #<Hash:0x007ff3625a4800>
Run Code Online (Sandbox Code Playgroud)
这就是我的response.code被调用的地方
def handle_response(response)
response_base = response['TwilioResponse']
if response_base.include?('RestException')
response_base['RestException']
else …Run Code Online (Sandbox Code Playgroud) rspec ×2
ruby ×2
activerecord ×1
exotel-api ×1
httparty ×1
mysql ×1
rubygems ×1
sidekiq ×1
unit-testing ×1