在user_spec.rb文件中我有这些相同的上下文,我想用干方法重新编码它。rails rspec上下文方法
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 [email protected]
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 [email protected]
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 [email protected]
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 [email protected]
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 [email protected]
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 [email protected]
end
end
end
你把这个代码放在哪里? – 2013-05-10 15:30:44
描述块内部 – 2013-05-10 15:32:07