问题描述
我想测试一个简单的类,它遍历哈希数组,并只返回状态为 Pending 且更新时间超过 2 天的类.
I want to test simple class which iterate through array of hashes and return only those with status Pending which were updated more than 2 days ago.
class FetchPending
PROJECT_KEY = 'TPFJT'
TWO_DAYS = Time.now - 2 * 24 * 60 * 60
def call
project.select do |issue|
issue.fields.dig('status', 'name') == 'Pending' &&
DateTime.parse(issue.fields.dig('updated')) < TWO_DAYS
end
end
private
def project
@project ||= Jira::ProjectConnection.new(PROJECT_KEY).call
end
end
如何测试 fields
方法,它是 Jira-Ruby gem 的方法.我认为它 来自这里(字段类gem 资源) 因为我在其他地方找不到 fields
方法.
How to test fields
method which is a method of Jira-Ruby gem. I think it comes from here (Field class in resource of gem) because nowhere else have I found fields
method.
以下是我调试后的想法:
Here are my thoughts after debugging:
project.class
- 数组
issue.class
- JIRA::Resource::Issue
issue.class
- JIRA::Resource::Issue
我的自然想法是:
before do
# (...) some other mocks
allow(JIRA::Resource::Issue).to receive(:fields)
end
但我收到一个错误:
失败/错误:允许(JIRA::Resource::Issue).to receive(:fields)
JIRA::Resource::Issue 没有实现:fields
JIRA::Resource::Issue does not implement: fields
我一直在努力解决这个问题 DAYS,我在这里非常绝望.如何模拟这个方法?
I have been struggling with this problem for DAYS, I'm pretty desperate here. How to mock this method?
这是我的其余规格:
RSpec.describe FetchPending do
subject { described_class.new }
let(:project_hash) do
[
{
'key': 'TP-47',
'fields': {
'status': {
'name': 'Pending'
},
'assignee': {
'name': 'michael.kelso',
'emailAddress': '[email protected]'
},
'updated': '2020-02-19T13:20:50.539+0100'
}
}
]
end
let(:project) { instance_double(Jira::ProjectConnection) }
before do
allow(Jira::ProjectConnection).to receive(:new).with(described_class::PROJECT_KEY).and_return(project)
allow(project).to receive(:call).and_return(project_hash)
allow(JIRA::Resource::Issue).to receive(:fields)
end
it 'return project hash' do
expect(subject.call).include(key[:'TP-47'])
end
推荐答案
and_return 通常用于返回值(例如字符串或整数)或值序列,但有时用于对象需要使用 block.此外,如果 call
是返回 project_hash 值的 Jira::ProjectConnection
对象上的有效方法,则您可以在声明实例 double 时直接模拟其行为(此功能从 Relish 文档中不清楚,因为它们有点可怕).这样的事情可能会奏效:
and_return is generally used for returning a value (such as a string or an integer) or sequence of values, but for objects you sometimes need use a block. Additionally, if call
is a valid method on a Jira::ProjectConnection
object that returns the value of project_hash, you can directly mock its behavior when declaring your instance double (this functionality is unclear from the Relish docs bc they are kinda terrible). Something like this will probably work:
let(:project) { instance_double(Jira::ProjectConnection, call: project_hash) }
before do
# Ensure new proj conns always return mocked 'project' obj
allow(Jira::ProjectConnection).to receive(:new).with(
described_class::PROJECT_KEY
) { project }
end
如果还是不行,尝试暂时用anything
替换scribed_class::PROJECT_KEY
进行调试;这可以帮助您确认是否指定了发送给 new
的错误参数.
If it still doesn't work, try temporarily replacing described_class::PROJECT_KEY
with anything
to debug; this can help you confirm if you specified the wrong arg(s) being sent to new
.
关于错误信息,它看起来像 JIRA::Resource::Issue 没有 fields
属性/方法,尽管 fields
似乎嵌套在 attrs
中?JIRA::Resource::Project#issues
方法还将 JSON 中的问题转换为问题对象,因此如果您使用该方法,则需要更改 project_hash
的内容.
With regard to the error message, it looks like JIRA::Resource::Issue doesn't have a fields
attribute/method, though fields
appears to be nested in attrs
? The JIRA::Resource::Project#issues
method also translates the issues in the JSON into Issue objects, so if you're using that method you will need to change the contents of project_hash
.
这篇关于选择循环内的 RSpec 模拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!