Skip to content

Commit

Permalink
Correct rspec expectations
Browse files Browse the repository at this point in the history
Signed-off-by: Chad Wilson <chadw@thoughtworks.com>
  • Loading branch information
chadlwilson committed Jun 22, 2024
1 parent e4a4eee commit ba22ea9
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 127 deletions.
44 changes: 22 additions & 22 deletions src/spec/ruby/action_controller/session/java_servlet_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
end

it "should raise an error if the servlet request is not present" do
lambda { @session_store.call({}) }.should raise_error
expect { @session_store.call({}) }.to raise_error(RuntimeError)
end

it "should do nothing if the session is not accessed" do
Expand All @@ -57,7 +57,7 @@
it "should load the session when accessed" do
@request.should_receive(:getSession).with(false).and_return @session
@session.stub(:setAttribute); @session.stub(:getCreationTime).and_return 1
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']['foo']
end
@session_store.call(@env)
Expand All @@ -73,7 +73,7 @@
it "should report session loaded when accessed" do
@request.should_receive(:getSession).with(false).and_return @session
@session.stub(:setAttribute); @session.stub(:getCreationTime).and_return 1
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']['foo']
end
@session_store.call(@env)
Expand All @@ -84,7 +84,7 @@
it "should use custom session hash when loading session" do
@request.should_receive(:getSession).with(false).and_return @session
@session.stub(:setAttribute); @session.stub(:getCreationTime).and_return 1
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']["foo"] = "bar"
end
@session_store.call(@env)
Expand All @@ -106,7 +106,7 @@
@session.should_receive(:getAttributeNames).and_return [session_key]
@session.should_receive(:getAttribute).with(session_key).and_return marshal_data.to_java_bytes
@session.stub(:setAttribute); @session.stub(:getCreationTime).and_return 1
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']["foo"].should == 1
env['rack.session']["bar"].should == true
end
Expand All @@ -120,7 +120,7 @@
@session.should_receive(:getAttribute).with("foo").and_return hash["foo"]
@session.should_receive(:getAttribute).with("bar").and_return hash["bar"]
@session.stub(:setAttribute); @session.stub(:getCreationTime).and_return 1
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']["foo"].should == hash["foo"]
env['rack.session']["bar"].should == hash["bar"]
end
Expand All @@ -132,7 +132,7 @@
@session.should_receive(:getAttributeNames).and_return ["foo"]
@session.should_receive(:getAttribute).with("foo").and_return java.lang.Object.new
@session.stub(:setAttribute); @session.stub(:getCreationTime).and_return 1
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']["foo"].should be_kind_of(java.lang.Object)
end
@session_store.call(@env)
Expand All @@ -143,7 +143,7 @@
@session.stub(:getAttribute).and_return nil; @session.stub(:getCreationTime).and_return 1
@session.should_receive(:setAttribute).with(ActionController::Session::JavaServletStore::RAILS_SESSION_KEY,
an_instance_of(Java::byte[]))
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']['foo'] = Object.new
end
@session_store.call(@env)
Expand All @@ -154,7 +154,7 @@
@request.should_receive(:getSession).with(true).ordered.and_return @session
@session.should_receive(:setAttribute).with(ActionController::Session::JavaServletStore::RAILS_SESSION_KEY,
an_instance_of(Java::byte[]))
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']['foo'] = Object.new
end
@session_store.call(@env)
Expand All @@ -164,7 +164,7 @@
@request.should_receive(:getSession).with(false).and_return @session
@session.stub(:setAttribute); @session.stub(:getCreationTime).and_return 1
@session.should_receive(:setAttribute).with("foo", "bar")
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']["foo"] = "bar"
end
@session_store.call(@env)
Expand All @@ -176,7 +176,7 @@
@session.should_receive(:setAttribute).with("foo", true)
@session.should_receive(:setAttribute).with("bar", 20)
@session.should_receive(:setAttribute).with("baz", false)
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']["foo"] = true
env['rack.session']["bar"] = 20
env['rack.session']["baz"] = false
Expand All @@ -188,7 +188,7 @@
@request.should_receive(:getSession).with(false).and_return @session
@session.stub(:setAttribute); @session.stub(:getCreationTime).and_return 1
@session.should_receive(:setAttribute).with("foo", an_instance_of(java.lang.Object))
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']["foo"] = java.lang.Object.new
end
@session_store.call(@env)
Expand All @@ -200,7 +200,7 @@
@session.stub(:setAttribute); @session.stub(:getCreationTime).and_return 1
@session.should_receive(:removeAttribute).with("foo")
@session.should_receive(:removeAttribute).with("baz")
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session'].delete('foo')
env['rack.session']['baz'] = nil
env['rack.session']['bar'] = 'x'
Expand All @@ -212,7 +212,7 @@
@request.should_receive(:getSession).with(false).and_return @session
@session.stub(:getId).and_return(nil)
@session.should_receive(:invalidate).ordered
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session.options'].delete(:id)
#env['rack.session'] = new_session_hash(env)
env['rack.session'].send :load!
Expand All @@ -224,11 +224,11 @@
session = double_http_session(nil); session.invalidate
@request.should_receive(:getSession).with(false).and_return session

@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session.options'].delete(:id)
env['rack.session'].send :load!
end
expect( lambda { @session_store.call(@env) } ).to_not raise_error
expect { @session_store.call(@env) }.to_not raise_error
end

it "should handle session with an invalid servlet session" do
Expand All @@ -240,15 +240,15 @@
@request.should_receive(:getSession).ordered.
with(true).and_return new_session = double_http_session

@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session']['foo'] = 'bar'
end
@session_store.call(@env)
end

it "should do nothing on session reset if no session is established" do
@request.should_receive(:getSession).with(false).and_return nil
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session.options'].delete(:id)
env['rack.session'] = new_session_hash(env) # not loaded?
end
Expand All @@ -260,7 +260,7 @@
@request.should_receive(:getSession).and_return @session
@session.should_receive(:getLastAccessedTime).and_return time
@session.stub(:setAttribute)
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session'].getLastAccessedTime.should == time
lambda { env['rack.session'].blah_blah }.should raise_error(NoMethodError)
end
Expand All @@ -274,7 +274,7 @@
new_session = double_http_session
@request.should_receive(:getSession).ordered.with(true).and_return(new_session)

@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session.options'] = { :id => sid, :renew => true, :defer => true }
env['rack.session']['_csrf_token'] = 'v3PrzsdkWug9Q3xCthKkEzUMbZSzgQ9Bt+43lH0bEF8='
end
Expand All @@ -292,11 +292,11 @@
it "handles the skip session option" do
@request.should_receive(:getSession).with(false).and_return @session
@session.should_not_receive(:setAttribute)
@app.should_receive(:call).and_return do |env|
@app.should_receive(:call) do |env|
env['rack.session.options'][:skip] = true
env['rack.session']['foo'] = 'bar'
end
expect( lambda { @session_store.call(@env) } ).to_not raise_error
expect { @session_store.call(@env) }.to_not raise_error
end

private
Expand Down
2 changes: 1 addition & 1 deletion src/spec/ruby/cgi/session/java_servlet_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def session_store

it "should raise an error if the servlet request is not present" do
@options.delete("java_servlet_request")
lambda { session_store }.should raise_error
expect { session_store }.to raise_error(RuntimeError)
end

describe "#restore" do
Expand Down
6 changes: 3 additions & 3 deletions src/spec/ruby/jruby/rack/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SomeKlass < Object
end

it "constantizes strictly" do
expect( lambda { constantize('Some', A::B) } ).to raise_error(NameError)
expect { constantize('Some', A::B) }.to raise_error(NameError)
end

it "constantizes non-stricly from Object (parent) context" do
Expand Down Expand Up @@ -95,8 +95,8 @@ class SomeKlass < Object

it "resolves a constant" do
expect( resolve_constant("JRuby::Rack::Helpers::Some") ).to be_a Class
expect( lambda { resolve_constant("JRuby::Rack::Helpers::Missing") }).to raise_error NameError
expect( lambda { resolve_constant("JRuby::Rack::Helpers::Another") }).to raise_error NameError
expect { resolve_constant("JRuby::Rack::Helpers::Missing") }.to raise_error NameError
expect { resolve_constant("JRuby::Rack::Helpers::Another") }.to raise_error NameError
end

end
10 changes: 5 additions & 5 deletions src/spec/ruby/jruby/rack/queues_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ def mock_message(text)
@registry.register_listener "FooQ", listener
@queue_manager.should_receive(:close).with "FooQ"
@registry.unregister_listener(listener)
lambda { @registry.receive_message("FooQ", mock_message("msg")) }.should raise_error
lambda { @registry.receive_message("FooQ", mock_message("msg")) }.should raise_error(RuntimeError)
end

it "#receive_message should raise an exception if there is no listener for the queue" do
lambda { @registry.receive_message("NoQ", "hi") }.should raise_error
lambda { @registry.receive_message("NoQ", "hi") }.should raise_error(RuntimeError)
end

it "#register_listener should allow multiple listeners per queue" do
Expand Down Expand Up @@ -175,7 +175,7 @@ def mock_message(text)
it "should unmarshal the message if the marshal payload property is set" do
@message.should_receive(:getBooleanProperty).with(JRuby::Rack::Queues::MARSHAL_PAYLOAD).and_return true
first = false
@message.should_receive(:readBytes).twice.and_return do |byte_array|
@message.should_receive(:readBytes).twice do |byte_array|
if first
-1
else
Expand Down Expand Up @@ -233,13 +233,13 @@ def on_message(msg)
@servlet_context.should_receive(:log).with(/something went wrong/)
lambda do
JRuby::Rack::Queues::MessageDispatcher.new(@listener).dispatch(@message)
end.should raise_error
end.should raise_error(RuntimeError)
end

it "should raise an exception if it was unable to dispatch to anything" do
@message.stub(:getBooleanProperty).and_return false
lambda do
JRuby::Rack::Queues::MessageDispatcher.new(@listener).dispatch(@message)
end.should raise_error
end.should raise_error(RuntimeError)
end
end
14 changes: 7 additions & 7 deletions src/spec/ruby/jruby/rack/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class << value; undef_method :each; end if value.respond_to?(:each)
response.write_headers(response_environment)

times = 0
stream.should_receive(:write).exactly(6).times.with do |bytes|
stream.should_receive(:write).exactly(6).times do |bytes|
str = String.from_java_bytes(bytes)
str = str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
case times += 1
Expand Down Expand Up @@ -223,7 +223,7 @@ class << value; undef_method :each; end if value.respond_to?(:each)
response.write_headers(response_environment)

times = 0
stream.should_receive(:write).exactly(3).times.with do |bytes|
stream.should_receive(:write).exactly(3).times do |bytes|
str = String.from_java_bytes(bytes)
case times += 1
when 1 then str.should == "1\r\n1\r\n"
Expand Down Expand Up @@ -258,7 +258,7 @@ class << value; undef_method :each; end if value.respond_to?(:each)
response.write_headers(response_environment)

times = 0
stream.should_receive(:write).exactly(5).times.with do |bytes|
stream.should_receive(:write).exactly(5).times do |bytes|
str = String.from_java_bytes(bytes)
case times += 1
when 1 then str.should == "1"
Expand Down Expand Up @@ -404,7 +404,7 @@ def wrap_file_body(path) # Rails style when doing #send_file
path = File.expand_path('../../files/image.jpg', File.dirname(__FILE__))

response = JRuby::Rack::Response.new [ 200, {}, FileBody.new(path) ]
response.should_receive(:send_file).with do |path, response|
response.should_receive(:send_file) do |path, response|
expect( path ).to eql path
expect( response).to be response_environment
end
Expand Down Expand Up @@ -526,13 +526,13 @@ def with_swallow_client_abort(client_abort = true)

private

def update_response_headers(headers, response)
def update_response_headers(headers)
response.to_java.getHeaders.update(headers)
end

def new_response_environment(servlet_response)
def new_response_environment(this_servlet_response = servlet_response)
org.jruby.rack.RackResponseEnvironment.impl do |name, *args|
servlet_response.send(name, *args)
this_servlet_response.send(name, *args)
end
end

Expand Down
Loading

0 comments on commit ba22ea9

Please sign in to comment.