RSpec: Stub controller method in request spec
You're supposed to be on vacation.
I think the right way is to avoid stubbing as much as you can in a request spec, doorkeeper needs a token to authorize so I'd do something like:
describe 'Items', type: :request do
describe 'GET /items' do
let(:application) { FactoryBot.create :oauth_application }
let(:user) { FactoryBot.create :user }
let(:token) { FactoryBot.create :access_token, application: application, resource_owner_id: user.id }
before do
get '/items', access_token: token.token
@parsed_body = JSON.parse(response.body)
end
it 'includes all of the items' do
expect(@parsed_body).to include(item_1)
expect(@parsed_body).to include(item_2)
end
end
end
Here are some examples of what those factories might look like.
Lastly, nice SO points!
Rspec: how to execute ApplicationController method from feature spec
Remember that get_component
is an instance method, not a class method, so you can only call it on an ApplicationController object, not on the class itself. Try this instead...
component = ApplicationController.new.get_component( slug )
Correct way of stubbing method call in RSpec request
The point is that authenticate_user
assigns user to the variable (and you use it later). Please try:
allow(DecodeAuthenticationCommand).to receive_message_chain(:call, :result).and_return(user)
With the test double, you will have to define all methods for the user, such as contracts
. Also, you are checking if the contract was created - in my opinion, it is perfectly fine to use a real object for the user
.
Stub helper method for request spec in rails / rspec
Helpers are typically used to clean up presentation "logic", so I wouldn't put something like a call to Amazon's API in a helper method.
Instead, move that method to a plain old Ruby class which you can call from your controller.
An example might be:
class AmazonBookRetriever
def get_books_from_amazon
#code here
end
end
Then your controller can call it:
def resources
@books = AmazonBookRetriever.new.get_books_from_amazon(params[:search_term])
end
This should make mocking a lot easier. You can stub #new
on the AmazonBookRetriever
to return a mock, and verify that it receives the get_books_from_amazon
message.
Stubbing authentication in request spec
A request spec is a thin wrapper around ActionDispatch::IntegrationTest
, which doesn't work like controller specs (which wrap ActionController::TestCase
). Even though there is a session method available, I don't think it is supported (i.e. it's probably there because a module that gets included for other utilities also includes that method).
I'd recommend logging in by posting to whatever action you use to authenticate users. If you make the password 'password' (for example) for all the User factories, then you can do something like this:
def login(user)
post login_path, :login => user.login, :password => 'password'
end
How to test ApplicationController method defined also as a helper method?
You can use an anonymous controller to test your ApplicationController, as describe in the RSpec documentation. There's also a section on testing helpers.
Related Topics
Heroku: Gemfile.Lock Is Required Issue
Is There a Cucumber Hook to Run Before and After Each Feature
Show Full Path Name of the Ruby File When It Get Loaded
Parse Email Addresses for "From" and "To" Fields in Ruby
What Does ::Myclass Ruby Scope Operator Do
Ruby-Debug19 on Ruby-1.9.3-Preview1
Problems Setting a Custom Primary Key in a Rails 4 Migration
Sinatra Clears Session on Post
Why Is Gets Throwing an Error When Arguments Are Passed to My Ruby Script
Heroku App Crash H10 - Bash: Bin/Rails: No Such File or Directory
Set Ruby Variable If It Is Not Already Defined