在 user_spec.rb 文件中,我有这些相同的上下文,我想用干法重新编码。
context 'when website adress starts with ' do
it 'http://, it should validate length of website' do
@profile.website = "x" * 393 # with appending http to website url its length will be 400.
assert @profile.save
end
it 'http://, it should not validate length of website' do
@profile.website = "x" * 394 # with appending http to website url its length will be 401.It should be failed.
assert !@profile.save
end
it 'https://, it should validate length of website' do
@profile.website = "https://" + "x" * 392 # with appending http to website url its length will be 400.
assert @profile.save
end
it 'https://, it should not validate length of website' do
@profile.website = "https://" + "x" * 393 # with appending http to website url its length will be 401.It should be failed.
assert !@profile.save
end
end
context 'when blog adress starts with ' do
it 'http://, it should validate length of blog' do
@profile.blog = "x" * 393 # with appending http to blog url its length will be 400.
assert @profile.save
end
it 'http://, it should not validate length of blog' do
@profile.blog = "x" * 394 # with appending http to blog url its length will be 401.It should be failed.
assert !@profile.save
end
it 'https://, it should validate length of blog' do
@profile.blog = "https://" + "x" * 392 # with appending http to blog url its length will be 400.
assert @profile.save
end
it 'https://, it should not validate length of blog' do
@profile.blog = "https://" + "x" * 393 # with appending http to blog url its length will be 401.It should be failed.
assert !@profile.save
end
end
有什么办法可以这样写吗?我想同时用2个方法。当我在下面编写代码并调用时,should_validate_length_of('website')
我有 undefined local variable or method
should_validate_length_of'` 错误
def should_validate_length_of(dummy)
context 'when website adress starts with ' do
it 'http://, it should validate length of website' do
@profile.dummy = "x" * 393 # with appending http to website url its length will be 400.
assert @profile.save
end
it 'http://, it should not validate length of website' do
@profile.dummy = "x" * 394 # with appending http to website url its length will be 401.It should be failed.
assert !@profile.save
end
it 'https://, it should validate length of website' do
@profile.dummy = "https://" + "x" * 392 # with appending http to website url its length will be 400.
assert @profile.save
end
it 'https://, it should not validate length of website' do
@profile.dummy = "https://" + "x" * 393 # with appending http to website url its length will be 401.It should be failed.
assert !@profile.save
end
end
end