【发布时间】:2018-05-01 13:27:40
【问题描述】:
尝试使用 Rspec 共享示例来测试两个类似 URL 的属性:
spec/entity_spec.rb
require 'entity'
require 'shared_examples/a_url'
describe Entity do
# create valid subject
subject { described_class.new(name: 'foobar') }
context ':url attribute' do
it_behaves_like "a URL"
end
context ':wikipedia_url attribute' do
it_behaves_like "a URL"
end
end
spec/shared_examples/a_url.rb
shared_examples_for('a URL') do |method|
it "ensures that :#{method} does not exceed 255 characters" do
subject.send(:method=, 'http://' + '@' * 256)
expect(subject).to_not be_valid
end
it "ensures that :#{method} do not accept other schemes" do
subject.send(:method=, 'ftp://foobar.com')
expect(subject).to_not be_valid
end
it "ensures that :#{method} accepts http://" do
subject.send(:method=, 'http://foobar.com')
expect(subject).to be_valid
end
it "ensures that :#{method} accepts https://" do
subject.send(:method=, 'https://foobar.com')
expect(subject).to be_valid
end
end
显然我需要将:url 和:wikipedia_url 属性的引用发送到共享示例,但是如何?
【问题讨论】: