Rails 4 rspec UrlGenerationError - ruby-on-rails

I am testing a Rails app that has been upgraded to Rails 4.2 using rspec 3.4.1. In this app I have the following route (as printed by rake routes):
contact_g GET /foo/contact/:legs_trip_id/:user_id(.:format)
foo#find_edit { :trip_type_id=>"123", :user_id=>"0", :locale=>"en", :format=>/html|js|json/, :legs_trip_id=>/\d+/}
In my app, in development and production modes, this route works fine. But in tests I get the following error when trying to use the route. Example spec:
describe "#find_edit, logged in:" do
before do
#c1 = mock_model(Foo, :id => 1059)
# mock a User, set up session and expectations to mimick a logged in user
#u = pseudo_login
end
it "..." do
expect(#u).to receive # ...
get :find_edit, :legs_trip_id => "123", :pn_id => 252, :sa_id => 1923, :email => 1, :ee_id => "bla", :trip_type_id => "123", user_id: #u.id
expect(response).to redirect_to(...)
end
As a controller test, at the "get" line rspec throws this error:
ActionController::UrlGenerationError:
No route matches { :action=>"find_edit", :controller=>"foo", :ee_id=>"bla", :email=>"1", :legs_trip_id=>"123", :pn_id=>"252", :sa_id=>"1923", :trip_type_id=>"123", :user_id=>"19" }
As a feature or integration specI can't access request.session to login my mocked user and thus can't get the tests to start at all:
undefined local variable or method `request' for #<RSpec::ExampleGroups::FindingByLTAndUserIDFindEditLoggedIn:...>
in this helper code that is called during spec setup:
request.session[:user_id] = mocked_user.id
How can I run these specs? It used to work in Rails 3.x.

Related

Running minitest controller get ActionController::UrlGenerationError: No route matches

I know, that with this topic more questions asket, but i don't found what i need.
Currently i'm updating rails app from 3.2.13 to 4.2.0 and after upgrading rails naturally fails tests. These tests are passed in 3.2.13
So, i have this route:
get '/catalogs/:article_id/get_applicability_by_brand/:brand_id', :to => 'catalogs#get_applicability_by_brand', constrains: { format: 'js' }, as: :catalog_get_applicability_by_brand
Result of rake routes like this:
catalog_get_applicability_by_brand GET /catalogs/:article_id/get_applicability_by_brand/:brand_id(.:format) catalogs#get_applicability_by_brand {:constrains=>{:format=>"js"}}
Controller action, it only render js.erb template:
def get_applicability_by_brand
#applicability = CatalogAccess::TecDoc.get_applicability_by_brand(params[:article_id], params[:brand_id])
end
Minitest controller test:
def test_get_applicability_by_brand_action
expected_applicability = [
{ 'model_name' => 'Model 1',
'name' => 'fake name',
'year_of_construct_from' => '2000',
'year_of_construct_to' => '2010',
'construction_type' => 'fake type' },
{ 'model_name' => 'Model 1',
'name' => 'fake name 2',
'year_of_construct_from' => '1991',
'year_of_construct_to' => '2005',
'construction_type' => 'fake type' }
]
CatalogAccess::TecDoc.expects(:get_applicability_by_brand).with('12', '23').returns expected_applicability
xhr :get, :get_applicability_by_brand, :article_id => '12', :brand_id => '23', :format => "js"
assert_response 200
assert_template 'get_applicability_by_brand'
assert_template :partial => '_tecdoc2_applicability'
end
Test error message is:
ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"get_applicability_by_brand", :article_id=>"12", :brand_id=>"23", :controller=>"catalogs", :format=>"js"}
I found that if append to my test option 'use_route', it will be pass, but get warning that seems not good solution
xhr :get, :get_applicability_by_brand, :article_id => '12', :brand_id => '23', :format => "js", :use_route => 'catalogs'
Warning message:
DEPRECATION WARNING: You are trying to generate the URL for a named route called "catalogs" but no such route was found. In the future, this will result in an `ActionController::UrlGenerationError` exception. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)
DEPRECATION WARNING: Passing the `use_route` option in functional tests are deprecated. Support for this option in the `process` method (and the related `get`, `head`, `post`, `patch`, `put` and `delete` helpers) will be removed in the next version without replacement. Functional tests are essentially unit tests for controllers and they should not require knowledge to how the application's routes are configured. Instead, you should explicitly pass the appropiate params to the `process` method. Previously the engines guide also contained an incorrect example that recommended using this option to test an engine's controllers within the dummy application. That recommendation was incorrect and has since been corrected. Instead, you should override the `#routes` variable in the test case with `Foo::Engine.routes`. See the updated engines guide for details. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)
DEPRECATION WARNING: You are trying to generate the URL for a named route called "catalogs" but no such route was found. In the future, this will result in an `ActionController::UrlGenerationError` exception. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)
Advise me please correct solution.
I'll hope your help, thanks!

Rails 4.1.12 and routes

I'm updating from Rails 4.1.8 to 4.1.12, and one spec is failing because of a route:
expected: "https://test.host/en/name-3/activities/3-activity-around-name-3/bookings/new?booking%5Bstart_date%5D=2015-07-03+08%3A38%3A39+UTC"
got: "https://test.host/en/name-3/activities/3-activity-around-name-3/bookings/new.name-3?booking%5Bstart_date%5D=2015-07-03+08%3A38%3A39+UTC" (using ==)
config/routes.rb:
get '*destination/activities/:id/bookings/new', to: 'bookings#new', as: :new_destination_activity_booking, constraints: DestinationConstraint.new
app/controllers/public/bookings_controller.rb:
def new
session[:traveler_return_to] = new_destination_activity_booking_url(#activity.destination, #activity, params)
spec/controllers/public/bookings_controller_spec.rb:
session[:traveler_return_to].should == new_destination_activity_booking_url(activity.destination, activity, booking: { start_date: start_date })
I tested that route in Rails console and it behaves fine. How can it add ".name-3" in specs?

Rspec Suite Failing on OmniAuth Mock Auth Hash

My Rspec tests are all passing individually but are failing as the whole suite.
I have narrowed the issue down to using the mock omniauth hash describe in spec/support/devise.rb:
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = {
"uid" => "1111",
"provider" => "facebook",
"credentials" => {
"token" => "token",
"secret" => "secret"
},
"extra" => {
"raw_info" => {
"name" => "Adam Waite",
"username" => "adamjwaite",
"email" => "adam#adam.com"
}
}
}
OmniAuth.config.add_mock(:facebook, OmniAuth.config.mock_auth[:facebook])
When I inspect OmniAuth.config.mock_auth[:facebook] just before the tests fails (using pry) it returns :invalid_credentials if run in the suite. If I run the same test in an individual test file it appears as it's displayed.
Here's the failing test in the registration method in my UsersController:
describe "GET :new" do
describe "as an unauthenticated user with a facebook omniauth session" do
before do
session[:omniauth_facebook] = OmniAuth.config.mock_auth[:facebook]
get :new
end
specify { assigns[:registering_with_facebook].should == true }
specify { assigns[:registering_with_twitter].should == false }
specify { response.should be_success }
end
end
It's also worth mentioning that the application function correctly too. I would just like the suite to pass.
Anyone shine any light on what's happening?
Whenever a test works in isolation, but fails with other tests, you have a test ordering issue. Some earlier test is changing global state and leaving it that way, which negatively affects this test.
I've created a small tool to help me find ordering issues in my own suites: rspec-search-and-destroy. It will take your test suite and bisect it until it finds the one test that is setting the bad global state. Of course, you can do this yourself by hand, but hopefully the tool can automate the drudgery.
Once you have found the earlier test, then you need to inspect it to figure out what global state is being set and how you can properly sandbox that change to just the test that needs it.

Rspec: add some header requests inside routing specs

I'm working on a Rails application having a REST API in JSON format and versioned (according to this excellent Ryan's cast: http://railscasts.com/episodes/350-rest-api-versioning).
For instance, there is a spec/requests spec:
require 'spec_helper'
describe "My Friends" do
describe "GET /my/friends.json" do
it "should get my_friends_path" do
get v1_my_friends_path, {}, {'HTTP_ACCEPT' => 'application/vnd.myapp+json; level=1'}
response.status.should be(401)
end
end
end
And it works well. But (keeping this example) how can we write the routing spec? For instance this spec isn't correct:
require 'spec_helper'
describe "friends routing" do
it "routes to #index" do
get("/my/friends.json", nil, {'HTTP_ACCEPT' => 'application/vnd.myapp+json; level=1'}).
should route_to({ action: "index",
controller: "api/v1/private/my/friends",
format: "json" })
end
end
I tried different ways (such as request.headers['Accept'] and #request.headers['Accept'], where request is undefined and #request is nil); I really don't see how to do.
I'm on Ruby 1.9.3, Rails 3.2.6 and rspec-rails 2.11.0. Thanks.
By combining the ideas from Cristophe's and Piotr's answers, I came up with a solution that worked for me. I'm using rspec and rails 3.0.
it 'should route like i want it to' do
Rack::MockRequest::DEFAULT_ENV["HTTP_ACCEPT"] = "*/*"
{get: "/foo/bar"}.
should route_to(
controller: 'foo',
action: 'bar',
)
Rack::MockRequest::DEFAULT_ENV.delete "HTTP_ACCEPT"
end
Currently you can't send addititional Headers in Routing specs, this is due to line 608 in actionpack-3.2.5/lib/action_dispatch/routing/route_set.rb where it says:
env = Rack::MockRequest.env_for(path, {:method => method})
path is your requested path "/my/friends.json" and method is :get
The resulting env contains something like the following:
{
"rack.version"=>[1, 1],
"rack.input"=>#<StringIO:0xb908f5c>,
"rack.errors"=>#<StringIO:0xb908fac>,
"rack.multithread"=>true,
"rack.multiprocess"=>true,
"rack.run_once"=>false,
"REQUEST_METHOD"=>"GET",
"SERVER_NAME"=>"your-url.com", # if path was http://your-url.com/
"SERVER_PORT"=>"80",
"QUERY_STRING"=>"",
"PATH_INFO"=>"/",
"rack.url_scheme"=>"http",
"HTTPS"=>"off",
"SCRIPT_NAME"=>"",
"CONTENT_LENGTH"=>"0"
}
If you are able to mock Rack::MockRequest::env_for it should be possible to inject other headers than the ones generated by env_for (see Hash above).
Other than that you are currently using the route_to matcher wrong, you should call it on a Hash where you specify the method and the path like this:
{ get: '/' }.should route_to(controller: 'main', action: 'index')
Let us know if you were able to Mock out that env_for and let it return your headers, would be nice to know.
Regards
Christoph
before do
ActionDispatch::TestRequest::DEFAULT_ENV["action_dispatch.request.accepts"] = "application/vnd.application-v1+json"
end
after do
ActionDispatch::TestRequest::DEFAULT_ENV.delete("action_dispatch.request.accepts")
end
You can using rspec's and_wrap_original to mock the Rack::MockRequest.env_for method:
expect(Rack::MockRequest).to receive(:env_for).and_wrap_original do |original_method, *args, &block|
original_method.call(*args, &block).tap { |hash| hash['HTTP_ACCEPT'] = 'application/vnd.myapp+json; level=1' }
end
For Rails 3 and 4 I had done the following in an RSpec around hook:
around do |example|
Rack::MockRequest::DEFAULT_ENV['HTTP_ACCEPT'] = 'application/vnd.vendor+json; version=1'
example.run
Rack::MockRequest::DEFAULT_ENV.delete 'HTTP_ACCEPT'
end
Since Rack >= 2.0.3 (used by Rails 5) the Rack::MockRequest::DEFAULT_ENV hash is frozen.
You can redefine the constant and use Kernel.silence_warnings to silence the Ruby warnings:
around do |example|
silence_warnings do
Rack::MockRequest::DEFAULT_ENV = Rack::MockRequest::DEFAULT_ENV.dup
end
Rack::MockRequest::DEFAULT_ENV['HTTP_ACCEPT'] = 'application/vnd.vendor+json; version=1'
example.run
Rack::MockRequest::DEFAULT_ENV.delete 'HTTP_ACCEPT'
end
It's a bit of hack but it works like a charm.

How Can I Tell Controller Specs to Use the Signed OAuth Request

I am building a 2-Legged OAuth provider for my api. Everything is hooked up properly and I can make signed calls from the rails console. The problem I have is that I am having trouble integrating OAuth into the controller_spec.
Here is an example of a working call on my server:
coneybeare $ rails c test
Loading test environment (Rails 3.2.0)
rails test: main
>> consumer = OAuth::Consumer.new("one_key", "MyString", :site => [REDACTED])
# => #<OAuth::Consumer:0x007f9d01252268 #key="one_key", #secret="MyString", #options={:signature_method=>"HMAC-SHA1", :request_token_path=>"/oauth/request_token", :authorize_path=>"/oauth/authorize", :access_token_path=>"/oauth/access_token", :proxy=>nil, :scheme=>:header, :http_method=>:post, :oauth_version=>"1.0", :site=>[REDACTED]}>
ruby: main
>> req = consumer.create_signed_request(:get, "/api/v1/client_applications.json", nil)
# => #<Net::HTTP::Get GET>
ruby: main
>> res = Net::HTTP.start([REDACTED]) {|http| http.request(req) }
# => #<Net::HTTPOK 200 OK readbody=true>
ruby: main
>> puts res.body
{"client_applications":[{"id":119059960,"name":"FooBar1","url":"http://test1.com"},{"id":504489040,"name":"FooBar2","url":"http://test2.com"}]}
# => nil
And here is what I am doing in my controller tests:
require 'oauth/client/action_controller_request'
describe Api::ClientApplicationsController do
include OAuthControllerSpecHelper
…
…
it "assigns all client_applications as #client_applications" do
consumer = OAuth::Consumer.new("one_key", "MyString", :site => [REDACTED])
ActionController::TestRequest.use_oauth=true
#request.configure_oauth(consumer)
#request.apply_oauth!
puts "request.env['Authorization'] = #{#request.env['Authorization']}"
get :index, {:api_version => 'v1', :format => :json}
response.should be_success # Just this for now until I can get authorization, then proper controller testing
end
end
The output of that test:
request.env['Authorization'] = OAuth oauth_consumer_key="one_key", oauth_nonce="gzAbvBSWyFtIYKfuokMAdu6VnH39EHeXvebbH2qUtE", oauth_signature="juBkJo5K0WLu9mYqHVC3Ar%2FATUs%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1328474800", oauth_version="1.0"
1) Api::ClientApplicationsController GET index assigns all client_applications as #client_applications
Failure/Error: response.should be_success
expected success? to return true, got false
And the corresponding server call from the rails log:
Processing by Api::ClientApplicationsController#index as JSON
Parameters: {"api_version"=>1}
Rendered text template (0.0ms)
Filter chain halted as #<OAuth::Controllers::ApplicationControllerMethods::Filter:0x007f85a51a8858 #options={:interactive=>false, :strategies=>:two_legged}, #strategies=[:two_legged]> rendered or redirected
Completed 401 Unauthorized in 15ms (Views: 14.1ms | ActiveRecord: 0.0ms)
(0.2ms) ROLLBACK
I just can't figure out why it's not working :/ Am I making an obvious mistake?
If you'd like to test it in a request spec and actually need to test without stubbing, you can build an OAuth consumer and sign a request like this:
#access_token = FactoryGirl.create :access_token
#consumer = OAuth::Consumer.new(#access_token.app.key, #access_token.app.secret, :site => "http://www.example.com/")
#path = "/path/to/request"
#request = #consumer.create_signed_request(:get, #path, OAuth::AccessToken.new(#consumer, #access_token.token, #access_token.secret))
get #path, nil, { 'HTTP_AUTHORIZATION' => #request.get_fields('authorization').first }
I would take a look as to how the Omniauth test helpers work, specifically these files: https://github.com/intridea/omniauth/tree/master/lib/omniauth/test. See their wiki page on integration testing for ideas of how this is set up. I realize that you're building a provider, not a client, but this may be a good starting point. Also, as some of the commenters have already said, I don't know if you can do this with a controller test; you may need a request or integration test to fully simulate the rack environment.
Turns out that the best way to test my controller was the simplest as well. Instead of trying to sign each test so the controller gets the right information (something that indeed does belong in a request spec not a controller spec), I figured out that I could just give the controller the information it needed manually.
To do this, I simply had to stub 2 methods:
fixtures :client_applications
before(:each) do
#client_application1 = client_applications(:client_application1)
Api::ClientApplicationsController::Authenticator.any_instance.stub(:allow?).and_return(true)
controller.stub(:client_application).and_return(#client_application1)
end
Stubbing the allow? method caused the rack auth to be fooled into thinking it was authenticated. allow? also set the client_application based on the credentials though, so I had to stub that as well. Now that the auth is out of the way, I can test my controller properly.

Resources