问题描述
我正在尝试为我的gem编写规范,该规范会生成otp并将其保存在db中.现在,我正在为此编写规格.所以基本上我有三种方法generate_otp!
,regenerate_otp!
,verify_otp(otp)
. generate_otp!
的作用是调用包含三个变量的方法generate_otp
I am trying to write specs for my gem, which generates otp and saves it in db. Now i am writing specs for it. so basically i have three methods generate_otp!
, regenerate_otp!
, verify_otp(otp)
. what generate_otp!
does is it calls a method generate_otp
which contains three variables
-
otp_code
-基本上是使用secure_random生成的随机值 -
otp_verified
-一个布尔值,用于设置otp是否通过验证的状态 -
otp_expiry_time
-设置otp的到期时间,该时间可以由Rails应用程序在配置中设置.
otp_code
- basically a random value generated using secure_randomotp_verified
- a boolean value to set the status whether otp is verified or nototp_expiry_time
- sets expiry time for otp which can be set by rails app in configuration.
这三个都是我数据库的列.
these all three are columns of my db also.
现在在generate_otp
之后,我正在调用active_records
的save
方法将值保存到db.
Now after generate_otp
i am calling active_records
's save
method to save values to db.
现在用于测试,我正在:memory:
中使用并将值存储在表中.测试后,我删除数据库.现在我不知道如何对随机生成的值进行存根并测试是否将值分配给所有三个列,即otp_code
,otp_verified
,otp_expiry_time
.
Now for testing i am using in :memory:
and storing values in table. after testing i am dropping the db. Now i am not getting how to stub the randomly generated value and test whether value is assigned or not to all three columns, i.e, otp_code
, otp_verified
, otp_expiry_time
.
这是我需要测试的方法代码.
Here is code for my methods which i need to test.
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
对此有任何帮助吗?我还检查了这个问题,但没有太大帮助.这是我第一次编写规范,而实际上对rspec没有太多经验.我还研究了官方文档模拟和存根,但我真的很困惑.
Any help with this ? I have also checked this question, but not much helpful. It's first time i am writing specs and really don't have that much experience with rspecs. I have also studied the official documentation for mock and stub, but i am really getting confused.
更新:otp_spec代码
update : otp_spec code
require 'spec_helper'
describe OtpGenerator do
let(:user) { build_mock_class.new() }
before(:all) { create_table }
after(:all) { drop_table }
describe '#generate_otp' do
it 'generate otp and save it' do
user.generate_otp!
end
end
describe '#regenerate_otp' do
it 'regenerate otp and save it' do
end
end
describe '#verify_otp' do
it 'verifies otp' do
end
end
def build_mock_class
Class.new(ActiveRecord::Base) do
self.table_name = 'mock_table'
include OtpGenerator
end
end
def create_table
ActiveRecord::Base.connection.create_table :mock_table do |t|
t.string :otp_code
t.boolean :otp_verified
t.datetime :otp_expiry_time
end
end
def drop_table
ActiveRecord::Base.connection.drop_table :mock_table
end
end
推荐答案
在rspec
中存根SecureRandom
方法的直接方法如下:
A direct approach to stubbing out the SecureRandom
method in rspec
would be as follows:
before { allow(SecureRandom).to receive(:hex).with(4).and_return('abcd1234') }
然后您可以检查'abcd1234'
是否存储在数据库中.为了保持测试DRY,您可能还希望将此变量作为变量引用,例如let(:random_value) { 'abcd1234' }
.
You can then check that 'abcd1234'
is stored in the database. In order to keep the test DRY, you may also wish to reference this as a variable, e.g. let(:random_value) { 'abcd1234' }
.
这篇关于使用secure_random在rspec中存根随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!