在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
答案 0 :(得分:0)
看看这段代码,我通过创建方法来减少代码量
要求'spec_helper'
describe SomeClass do
let(:inner_app) { ->(env){[200, {'Content-Type' => 'text/plain'}, ['All good!']]} }
let(:app) { SomeClass.new(inner_app) }
class << self
def method_tests(m, http_status, response_status)
it "returns a #{http_status} status" do
status, _, _ = app.send(m, response)
expect(status).to eq(http_status)
end
it "has application/json content type headers" do
_, headers, _ = app.send(m, response)
expect(headers).to include({'Content-Type' => 'application/json'})
end
it "returns a formatted body with status #{response_status}" do
_, _, body = app.send(m, response)
expect(body.first).to eq(response.merge!(status: response_status).to_json)
end
end
end
describe "#success" do
method_tests(:success, 200, :success)
end
describe "#unauthorized" do
method_tests(:unauthorized, 401, :error)
end
describe "#bad_request" do
method_tests(:bad_request, 400, :error)
end
end
方法本身必须是类方法。虽然我的测试与您的测试不同,但概念仍然相同。 :)