spec/unit/rack/validation/param_spec.rb in goliath-1.0.5 vs spec/unit/rack/validation/param_spec.rb in goliath-1.0.6

- old
+ new

@@ -6,43 +6,43 @@ @app = double('app').as_null_object @env = {'params' => {}} end it "should not allow invalid options" do - lambda { + expect { Goliath::Rack::Validation::Param.new(@app, {:key => 'user', :as => Class.new}) - }.should raise_error(Exception) + }.to raise_error(Exception) end it "raises if key is not supplied" do - lambda { + expect { Goliath::Rack::Validation::Param.new(@app) - }.should raise_error(Exception) + }.to raise_error(Exception) end it "uses a default value if optional is not supplied" do cv = Goliath::Rack::Validation::Param.new(@app, :key => 'key') - cv.optional.should be false + expect(cv.optional).to be false end it "should have default and message be optional" do cv = nil - lambda { + expect { cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'flag', :as => Goliath::Rack::Types::Boolean}) - }.should_not raise_error + }.not_to raise_error - cv.default.should be_nil - cv.message.should_not be_nil + expect(cv.default).to be_nil + expect(cv.message).not_to be_nil end it "should fail if given an invalid option" do cv = nil - lambda { + expect { cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'flag', :as => Goliath::Rack::Types::Boolean, :animal => :monkey}) - }.should raise_error('Unknown options: {:animal=>:monkey}') + }.to raise_error('Unknown options: {:animal=>:monkey}') end context "fetch_key" do before do @cv = Goliath::Rack::Validation::Param.new(@app, @@ -55,44 +55,44 @@ 'credentials' => { 'login' => "mike" } } } - @cv.fetch_key(params).should == "mike" + expect(@cv.fetch_key(params)).to eq("mike") end it "should return nil given an incorrect params" do params = { 'data' => { 'credentials' => { 'login2' => "mike" } } } - @cv.fetch_key(params).should be_nil + expect(@cv.fetch_key(params)).to be_nil end it "should set value if given" do params = { 'data' => { 'credentials' => { 'login' => "mike" } } } - @cv.fetch_key(params, "tim").should == "tim" - params['data']['credentials']['login'].should == "tim" + expect(@cv.fetch_key(params, "tim")).to eq("tim") + expect(params['data']['credentials']['login']).to eq("tim") end end context "Required" do it 'defaults type and message' do @rp = Goliath::Rack::Validation::Param.new('app', :key => 'key') - @rp.type.should_not be_nil - @rp.type.should_not =~ /^\s*$/ - @rp.message.should == 'identifier missing' + expect(@rp.type).not_to be_nil + expect(@rp.type).not_to match(/^\s*$/) + expect(@rp.message).to eq('identifier missing') end context 'with middleware' do before(:each) do @@ -101,80 +101,80 @@ @rp = Goliath::Rack::Validation::Param.new(@app, {:type => 'Monkey', :key => 'mk', :message => 'is required'}) end it 'stores type and key options' do - @rp.type.should == 'Monkey' - @rp.key.should == 'mk' + expect(@rp.type).to eq('Monkey') + expect(@rp.key).to eq('mk') end it 'calls validation_error with a custom message' do - @rp.should_receive(:validation_error).with(anything, 'Monkey is required') + expect(@rp).to receive(:validation_error).with(anything, 'Monkey is required') @rp.call(@env) end it 'returns the app status, headers and body' do app_headers = {'Content-Type' => 'app'} app_body = {'b' => 'c'} - @app.should_receive(:call).and_return([201, app_headers, app_body]) + expect(@app).to receive(:call).and_return([201, app_headers, app_body]) @env['params']['mk'] = 'monkey' status, headers, body = @rp.call(@env) - status.should == 201 - headers.should == app_headers - body.should == app_body + expect(status).to eq(201) + expect(headers).to eq(app_headers) + expect(body).to eq(app_body) end context 'key_valid?' do it 'raises exception if the key is not provided' do - @rp.key_valid?(@env['params']).should be false + expect(@rp.key_valid?(@env['params'])).to be false end it 'raises exception if the key is blank' do @env['params']['mk'] = '' - @rp.key_valid?(@env['params']).should be false + expect(@rp.key_valid?(@env['params'])).to be false end it 'raises exception if the key is nil' do @env['params']['mk'] = nil - @rp.key_valid?(@env['params']).should be false + expect(@rp.key_valid?(@env['params'])).to be false end it 'handles an empty array' do @env['params']['mk'] = [] - @rp.key_valid?(@env['params']).should be false + expect(@rp.key_valid?(@env['params'])).to be false end it 'handles an array of nils' do @env['params']['mk'] = [nil, nil, nil] - @rp.key_valid?(@env['params']).should be false + expect(@rp.key_valid?(@env['params'])).to be false end it 'handles an array of blanks' do @env['params']['mk'] = ['', '', ''] - @rp.key_valid?(@env['params']).should be false + expect(@rp.key_valid?(@env['params'])).to be false end it "doesn't raise if the key provided" do @env['params']['mk'] = 'my value' - @rp.key_valid?(@env['params']).should be true + expect(@rp.key_valid?(@env['params'])).to be true end it "doesn't raise if the array contains valid data" do @env['params']['mk'] = [1, 2, 3, 4] - @rp.key_valid?(@env['params']).should be true + expect(@rp.key_valid?(@env['params'])).to be true end it "doesn't raise if the key provided is multiline and has blanks" do @env['params']['mk'] = "my\n \nvalue" - @rp.key_valid?(@env['params']).should be true + expect(@rp.key_valid?(@env['params'])).to be true end it "doesn't raise if the key provided is an array and contains multiline with blanks" do @env['params']['mk'] = ["my\n \nvalue", "my\n \nother\n \nvalue"] - @rp.key_valid?(@env['params']).should be true + expect(@rp.key_valid?(@env['params'])).to be true end end end context 'Nested keys tests' do @@ -192,22 +192,22 @@ 'login2' => "user", 'pass' => "password"} } } - @rp.key_valid?(@env['params']).should be false + expect(@rp.key_valid?(@env['params'])).to be false end it "return true if key is present" do @env['params'] = {'data' => { 'credentials' => { 'login' => "user", 'pass' => "password"} } } - @rp.key_valid?(@env['params']).should be true + expect(@rp.key_valid?(@env['params'])).to be true end end context 'Nested keys tests (with string)' do before do @@ -223,33 +223,33 @@ 'login2' => "user", 'pass' => "password"} } } - @rp.key_valid?(@env['params']).should be false + expect(@rp.key_valid?(@env['params'])).to be false end it "return true if key is present" do @env['params'] = {'data' => { 'credentials' => { 'login' => "user", 'pass' => "password"} } } - @rp.key_valid?(@env['params']).should be true + expect(@rp.key_valid?(@env['params'])).to be true end end end context "Coerce" do it "should only accept a class in the :as" do - lambda { + expect { Goliath::Rack::Validation::Param.new(@app, {:key => 'user', :as => "not a class"}) - }.should raise_error('Params as must be a class') + }.to raise_error('Params as must be a class') end context 'with middleware' do { Goliath::Rack::Types::Boolean => [['t', true], ['true', true], ['f', false], @@ -264,11 +264,11 @@ values.each do |value| it "should coerce #{type} from #{value.first} to #{value[1]}" do @env['params']['user'] = value.first cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'user', :as => type}) cv.call(@env) - @env['params']['user'].should == value[1] + expect(@env['params']['user']).to eq(value[1]) end end end { @@ -280,37 +280,37 @@ values.each do |value| it "should not coerce #{type} with #{value}" do @env['params']['user'] = value cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'user', :as => type}) result = cv.call(@env) - result.should be_an_instance_of(Array) - result.first.should == 400 - result.last.should have_key(:error) + expect(result).to be_an_instance_of(Array) + expect(result.first).to eq(400) + expect(result.last).to have_key(:error) end end end it "should not fail with a invalid input, given a default value" do cv = nil @env['params']['user'] = "boo" - lambda { + expect { cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'user', :as => Goliath::Rack::Types::Boolean , :default => 'default'}) - }.should_not raise_error + }.not_to raise_error @env['params']['user'] = 'default' end it "should be able to take a custom fail message" do @env['params']['user'] = "boo" cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'user', :as => Goliath::Rack::Types::Integer, :message => "custom message"}) result = cv.call(@env) - result.should be_an_instance_of(Array) - result.first.should == 400 - result.last.should have_key(:error) - result.last[:error].should == "custom message" + expect(result).to be_an_instance_of(Array) + expect(result.first).to eq(400) + expect(result.last).to have_key(:error) + expect(result.last[:error]).to eq("custom message") end end end @@ -318,126 +318,126 @@ it "should do required param + coerce (not nested)" do cv = Goliath::Rack::Validation::Param.new @app, :key => "login", :as => Goliath::Rack::Types::Integer @env['params']['login'] = "3" cv.call(@env) - @env['params']['login'].should == 3 + expect(@env['params']['login']).to eq(3) @env['params']['login'] = nil cv = Goliath::Rack::Validation::Param.new @app, :key => "login", :as => Goliath::Rack::Types::Integer result = cv.call(@env) - result.should be_an_instance_of(Array) - result.first.should == 400 - result.last.should have_key(:error) + expect(result).to be_an_instance_of(Array) + expect(result.first).to eq(400) + expect(result.last).to have_key(:error) end it "should do required param + coerce (nested)" do cv = Goliath::Rack::Validation::Param.new @app, :key => ['login', 'other'], :as => Goliath::Rack::Types::Integer @env['params']['login'] = {} @env['params']['login']['other'] = "3" cv.call(@env) - @env['params']['login']['other'].should == 3 + expect(@env['params']['login']['other']).to eq(3) @env['params']['login'] = {} @env['params']['login']['other'] = nil cv = Goliath::Rack::Validation::Param.new @app, :key => "login.other", :as => Goliath::Rack::Types::Integer result = cv.call(@env) - result.should be_an_instance_of(Array) - result.first.should == 400 - result.last.should have_key(:error) + expect(result).to be_an_instance_of(Array) + expect(result.first).to eq(400) + expect(result.last).to have_key(:error) end it "should do required param + not coerce (not nested)" do cv = Goliath::Rack::Validation::Param.new @app, :key => "login" @env['params']['login'] = "3" cv.call(@env) - @env['params']['login'].should == "3" + expect(@env['params']['login']).to eq("3") @env['params']['login'] = nil cv = Goliath::Rack::Validation::Param.new @app, :key => "login" result = cv.call(@env) - result.should be_an_instance_of(Array) - result.first.should == 400 - result.last.should have_key(:error) + expect(result).to be_an_instance_of(Array) + expect(result.first).to eq(400) + expect(result.last).to have_key(:error) end it "should do required param + not coerce (nested)" do cv = Goliath::Rack::Validation::Param.new @app, :key => ['login', 'other'] @env['params']['login'] = {} @env['params']['login']['other'] = "3" cv.call(@env) - @env['params']['login']['other'].should == "3" + expect(@env['params']['login']['other']).to eq("3") @env['params']['login'] = {} @env['params']['login']['other'] = nil cv = Goliath::Rack::Validation::Param.new @app, :key => "login.other" result = cv.call(@env) - result.should be_an_instance_of(Array) - result.first.should == 400 - result.last.should have_key(:error) + expect(result).to be_an_instance_of(Array) + expect(result.first).to eq(400) + expect(result.last).to have_key(:error) end it "should do optional param + coerce (not nested)" do cv = Goliath::Rack::Validation::Param.new @app, :key => "login", :as => Goliath::Rack::Types::Integer, :optional => true @env['params']['login'] = "3" cv.call(@env) - @env['params']['login'].should == 3 + expect(@env['params']['login']).to eq(3) @env['params']['login'] = nil cv = Goliath::Rack::Validation::Param.new @app, :key => "login", :as => Goliath::Rack::Types::Integer, :optional => true result = cv.call(@env) - result.should_not be_an_instance_of(Array) #implying its OK + expect(result).not_to be_an_instance_of(Array) #implying its OK end it "should do optional param + coerce (nested)" do cv = Goliath::Rack::Validation::Param.new @app, :key => "login.other", :as => Goliath::Rack::Types::Integer, :optional => true @env['params']['login'] = {} @env['params']['login']['other'] = "3" cv.call(@env) - @env['params']['login']['other'].should == 3 + expect(@env['params']['login']['other']).to eq(3) @env['params']['login'] = {} @env['params']['login']['other'] = nil cv = Goliath::Rack::Validation::Param.new @app, :key => ['login', 'other'], :as => Goliath::Rack::Types::Integer, :optional => true result = cv.call(@env) - result.should_not be_an_instance_of(Array) #implying its OK + expect(result).not_to be_an_instance_of(Array) #implying its OK end it "should do optional param and not coerce (not nested)" do cv = Goliath::Rack::Validation::Param.new @app, :key => "login", :optional => true @env['params']['login'] = "3" cv.call(@env) - @env['params']['login'].should == "3" + expect(@env['params']['login']).to eq("3") @env['params']['login'] = nil cv = Goliath::Rack::Validation::Param.new @app, :key => "login", :optional => true result = cv.call(@env) - result.should_not be_an_instance_of(Array) #implying its OK + expect(result).not_to be_an_instance_of(Array) #implying its OK end it "should do optional param and not coerce (nested)" do cv = Goliath::Rack::Validation::Param.new @app, :key => ['login', 'other'], :optional => true @env['params']['login'] = {} @env['params']['login']['other'] = "3" cv.call(@env) - @env['params']['login']['other'].should == "3" + expect(@env['params']['login']['other']).to eq("3") @env['params']['login'] = {} @env['params']['login']['other'] = nil cv = Goliath::Rack::Validation::Param.new @app, :key => "login.other", :optional => true result = cv.call(@env) - result.should_not be_an_instance_of(Array) #implying its OK + expect(result).not_to be_an_instance_of(Array) #implying its OK end end end