No route matches for inherited (create) action - ruby-on-rails

I have 2 versions of my API(1&2) the controller of api version 2 inherits from the controller of api version 1. The problem is, specs for version 1 are running fine, but when i run specs in version 2, it tells me
ActionController::UrlGenerationError: No route matches {:action=>"create", :controller=>"api/mobile/v2/samples"}
#version 1
class Api::Mobile::V1::SamplesController < ApplicationController
def create
#dummy
end
end
#version 2
require "meta_data"
class Api::Mobile::V2::SamplesController < Api::Mobile::V1::SamplesController
include MetaData
end
#spec for version 1
require 'rails_helper'
RSpec.describe Api::Mobile::V1::SamplesController, type: :controller do
describe "POST #create" do
it "performs a post" do
post :create
end
end
end
#spec for version 2
require 'rails_helper'
RSpec.describe Api::Mobile::V2::SamplesController, type: :controller do
describe "POST #create" do
it "performs a post" do
post :create
end
end
end

RSpec is looking for SamplesController (plural). Make sure you are specifying your routes using singular resource if that's what you have. Otherwise, specify plural form of sample as sample in an initializer.
I would just go with the flow and user plural form. But, obviously with the public API that's a real headache.

Related

Rspec test to request controller action but seems action wasn't executed

I am new to Rails and Rspec and trying to write spec to test one non RESTful controller action.
Spec code:
require 'rails_helper'
RSpec.describe ReportsController, type: :controller do
describe "GET /full_members" do
context "when current account doesn't have contacts" do
let!(:current_account) { create(:account) }
it "hit full_members action" do
expect(get: '/reports/full_members').to be_routable
expect(:get => "/reports/full_members").to route_to(
controller: "reports",
action: "full_members")
get :full_members
end
end
end
end
Controller code:
class ReportsController < ApplicationController
def full_members
require "pry"
binding.pry
set_select_options_full_members
#members = #current_account.contacts
end
...
I was expecting to execute spec and stop at the breaking point pry. But the actual result is spec passed and pry didn't work.
Finished in 0.17359 seconds (files took 1.16 seconds to load)
1 example, 0 failures
From rails routes I can see the route exists(the spec first two assertions also prove that)
~ยป rails routes | ag full_members
>> full_members_reports GET /reports/full_members(.:format)
My routes.rb
resources :reports, only: [:index] do
collection do
get 'full_members'
end
end
Can not figure out why the pry didn't work in controller action. Can anyone help with that? Thanks

Rspec routing test not passing

The below rspec test is not passing, I'm not sure what I'm doing wrong. It routes fine in the browser, but the test is not passing.
require 'spec_helper'
describe QueueVideosController do
describe "GET show" do
context "with authenticated users" do
it "routes /queue to the QueueVideos controller" do
expect(get("/queue")).to route_to("queue_videos#show")
end
end
end
end
From my controller:
class QueueVideosController < ApplicationController
def show
end
end
From my routes file:
get '/queue' => 'queue_videos#show'
Try using a different syntax:
expect(get: "/queue").to route_to(
controller: "queue_videos",
action: "show"
)

How to create a route for testing purposes?

I'm writing tests with rspec for my application controller in my rails app (written in Rails 4) and I'm running into a problem where it doesn't recognize the route for the HTTP request I'm sending. I know there's a way to do this using MyApp::Application.routes but I'm not able to get it working.
#application_controller_spec.rb
require 'spec_helper'
class TestController < ApplicationController
def index; end
end
describe TestController do
before(:each) do
#first_user = FactoryGirl.create(:user)
# this is to ensure that all before_filters are run
controller.stub(:first_time_user)
controller.stub(:current_user)
end
describe 'first_time_user' do
before(:each) do
controller.unstub(:first_time_user)
end
context 'is in db' do
before(:each) do
#user = FactoryGirl.create(:user)
controller.stub(:current_user).and_return(#user)
end
it 'should not redirect' do
get :index
response.should_not be_redirect
end
end
context 'is not in db' do
context 'session[:cas_user] does not exist' do
it 'should return nil' do
get :index
expect(assigns(:current_user)).to eq(nil)
end
end
it "should redirect_to new_user_path" do
controller.stub(:current_user, redirect: true).and_return(nil)
get :index
response.should be_redirect
end
end
end
The error I'm getting right now is
No route matches {:action=>"index", :controller=>"test"}
I would add the test#index route to config/routes.rb, but it doesn't recognize the Test Controller, so I want to do something like
MyApp::Application.routes.append do
controller :test do
get 'test/index' => :index
end
end
but I'm not sure where to add this or if this even works in rspec. Any help would be great!
If you are trying to test your ApplicationController, see this RSpec documentation about it. You will need to define methods like index inside the test, but it works well.

Specs for controller inside a module (versionist)

I'm creating an API in Rails and I use versionist to handle versions. I want to test API controllers, but I'm unable to create a valid request.
My controller:
class Api::V1::ItemsController < Api::V1::BaseController
def index
render json:'anything'
end
end
My spec:
describe Api::V1::ItemsController do
describe "#create" do
it "shows items" do
get :index, format: :json
end
end
end
routes.rb:
scope '/api' do
api_version(:module => "Api::V1", :path => {:value => "v1"}, :default => true) do
resources :items
end
end
The test doesn't check anything. Still, it raises an error:
Failure/Error: get :index, format: :json
ActionController::RoutingError:
No route matches {:format=>:json, :controller=>"api/v1/items", :action=>"index"}
I suppose that there is something wrong with the :controller key in the request, but I don't know how to fix it...
I was able to reproduce this locally. You need to move this to a request spec instead of a controller spec for this to work:
# spec/requests/api/v1/items_controller_spec.rb
describe Api::V1::ItemsController do
describe "#index" do
it "shows items" do
get '/api/v1/items.json'
# assert something
end
end
end
The versionist documentation says you need to do this when using the HTTP header or request parameter versioning strategies (https://github.com/bploetz/versionist#a-note-about-testing-when-using-the-http-header-or-request-parameter-strategies) but that's clearly not the case here. I'll file an issue to get this clarified in the documentation that you need to do this for all versioning strategies.

How to test refinery sessions controller

I've overridden Refinery's session controller and not even modified it yet, as I'm trying to write some specs for it.
The controller lives in app/controllers/refinery/sessions_controller:
module Refinery
class SessionsController < Devise::SessionsController
....
def create
super
rescue ::BCrypt::Errors::InvalidSalt, ::BCrypt::Errors::InvalidHash
flash[:error] = t('password_encryption', :scope => 'refinery.users.forgot')
redirect_to refinery.new_refinery_user_password_path
end
.....
I then am trying to write a spec against this in spec/controllers/refinery/sessions_controller_spec.rb:
require 'spec_helper'
describe Refinery::SessionsController do
it "should post ok" do
post :create
response.should be_success
end
end
However this is giving me an error
No route matches {:action=>"create", :controller=>"refinery/sessions"}
This confuses me as when I run rake routes, I get the following line:
refinery_user_session POST /refinery/users/login(.:format) refinery/sessions#create
Could anyone help?
Rails 3.2.8, refinery 2.0.8

Resources