You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been struggling with this for quite some time.
Sometimes, in rspec specs, I would like to have access to the same methods that are available inside routing blocks of my app.
For example, with this:
get'/'dologger.info"works"end
I would like to write a spec like this:
it'logs the request'doexpect(app.logger).toreceive(:info).with('works')get'/'end
The below file can be executed as is using:
$ rspec test.rb
if anyone has time to take a look and propose a solution.
# test.rb# Serverrequire'sinatra'set:bind,'0.0.0.0'set:port,3000get'/'dologger.info'works''OK'end# Specrequire'rack/test'require'rspec'require'logger'RSpec.configuredo |config|
defapp=Sinatra::ApplicationincludeRack::Test::Methodsenddescribe'GET /'doit'is logs the request'do# This does not work# expect(app.logger).to receive(:info).with('works')# This works, but is undesireableexpect_any_instance_of(Logger).toreceive(:info).with('works')get'/'expect(last_response).tobe_successfulexpect(last_response.body).toeq'OK'endend
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have been struggling with this for quite some time.
Sometimes, in rspec specs, I would like to have access to the same methods that are available inside routing blocks of my app.
For example, with this:
I would like to write a spec like this:
The below file can be executed as is using:
if anyone has time to take a look and propose a solution.
Thanks in advance if anyone can help.
Beta Was this translation helpful? Give feedback.
All reactions