Ruby on rails RSpec测试-slackbotruby
我是一名编程新手,我正在尝试对我使用Ruby为slack机器人制作的命令进行RSpec测试。 该命令使用下面的代码中显示的.where(Time.zone.now.begining_of_day..Time.zone.now.end_of_day)=>从数据库检索数据。 映射它们中的每一个,并为用户输出设置格式 代码: RSpec测试:Ruby on rails RSpec测试-slackbotruby,ruby-on-rails,ruby,rspec,rspec-rails,slack,Ruby On Rails,Ruby,Rspec,Rspec Rails,Slack,我是一名编程新手,我正在尝试对我使用Ruby为slack机器人制作的命令进行RSpec测试。 该命令使用下面的代码中显示的.where(Time.zone.now.begining_of_day..Time.zone.now.end_of_day)=>从数据库检索数据。 映射它们中的每一个,并为用户输出设置格式 代码: RSpec测试: RSpec.describe RecordCommand do describe '#call' do it 'retrieves data and
RSpec.describe RecordCommand do
describe '#call' do
it 'retrieves data and format it' do
StartCommand.new.call
expect(Answer.where).to eq()
end
end
正如您所看到的,我对如何在RSpec测试中实现代码有点迷茫,任何想法、提示或解决方案都会很棒!帮助
我使用Ruby和RubyonRails 4您可以使用gem在测试运行之间擦除数据库。它允许您根据需要在测试用例中创建记录。无论何时测试任何与时间相关的内容,都最好使用,因为它可以随时冻结测试用例并消除时间的不可预测性。假设你已经安装了这些-
it 'retrieves data and format it' do
# Freeze time at a point
base_time = Time.zone.now.beginning_of_day + 5.minutes
Timecop.freeze(base_time) do
# setup, create db records
# 2 in the current day, 1 in the previous day
Answer.create(slack_identifier: "s1", body: "b1", created_at: base_time)
Answer.create(slack_identifier: "s2", body: "b2", created_at: base_time + 1.minute)
Answer.create(slack_identifier: "s3", body: "b3", created_at: base_time - 1.day)
# call the method and verify the results
expect(StartCommand.new.call).to eq("s1:\nb1\n\ns2:\nb2")
end
end
您可能还需要将订单
添加到where
查询中。我不认为您可以保证ActiveRecord每次都以相同的顺序返回记录,除非您指定它(请参阅),并且指定它将使测试更加可靠
it 'retrieves data and format it' do
# Freeze time at a point
base_time = Time.zone.now.beginning_of_day + 5.minutes
Timecop.freeze(base_time) do
# setup, create db records
# 2 in the current day, 1 in the previous day
Answer.create(slack_identifier: "s1", body: "b1", created_at: base_time)
Answer.create(slack_identifier: "s2", body: "b2", created_at: base_time + 1.minute)
Answer.create(slack_identifier: "s3", body: "b3", created_at: base_time - 1.day)
# call the method and verify the results
expect(StartCommand.new.call).to eq("s1:\nb1\n\ns2:\nb2")
end
end