2013-05-10 51 views
0

在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 
+0

你把这个代码放在哪里? – 2013-05-10 15:30:44

+0

描述块内部 – 2013-05-10 15:32:07

回答

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 

该方法本身必须是一个类方法。尽管我的测试与您的测试不同,但这个概念仍然是一样的。 :)

+0

谢谢Leo.class <<我正在寻找自己。 – 2013-05-10 15:37:53

+0

请注意,您实际上可以在self.some_method前加上方法名称,它会做同样的事情 – 2013-05-10 17:09:03