【发布时间】:2014-01-26 13:17:25
【问题描述】:
我有外部 API 端点,比如说:http://www.fake_me_hard.com/api。我想从我的应用程序中对此进行一些调用。
Endpoint 接受以下结构作为参数:
{
:amount => amount,
:backurl => root_path,
:language => locale,
:orderid => order_id,
:pm => payment_method,
:accept_url => "/payment/success",
:exception_url => "/payment/failure",
}
负责收集此哈希的方法是EndpointRequestCollector.give_me_hash。
我应该如何测试give_me_hash 是否返回正确的结构?
我也可以使用相同的策略在 specs 和 class 中创建此结构:
class EndpointRequestsCollector
def self.give_me_hash
{
#....collecting hash #1
}
end
end
describe EndpointRequestCollector do
context '.give_me_hash' do
it 'returns proper structure' do
expect(described_class.give_me_hash).to eq(
{
#... collecting hash #2
}
)
end
end
end
...但它会在 2 个地方重复相同的代码,并且不会测试任何东西。
你知道解决这个问题的好方法吗?
【问题讨论】:
-
我不理解你的问题
标签: ruby-on-rails ruby unit-testing rspec