I want to test my namespaced controllers but when I raise controller nothing raised and test is passing. I get no routes error. They all work fine. But it must raise error. Actually it must call index action but it does not. What is the cause?
dashboard_controller_spec.rb
require 'spec_helper'
describe Admin::DashboardController do
it "gets index" do
get :index
end
end
routes.rb
namespace :admin do
match 'dashboard' => 'dashboard#index', :as => :dashboard
end
dashboard_controller.rb
class Admin::DashboardController < Admin::ApplicationController
def index
raise "asd"
end
end
Hi you can write the route path as follow so get index action
describe :route do
subject { {get: "/Admin/dashboardes"} }
it { should route_to(controller: "Admin/dashboardes", action: "index") }
end
like example :
describe "#index" do
describe :route do
subject { {get: "/administration/users"} }
it { should route_to(controller: "administration/users", action: "index") }
end
end
Related
I'm trying to write a controller test which tests a subdomain constraint. However, I'm unable to get RSpec to set the subdomain and return an error if the subdomain isn't accurate.
I'm using Rails 4.2.6 and RSpec ~3.4
routes.rb
namespace :frontend_api do
constraints subdomain: 'frontend-api' do
resources :events, only: [:index]
end
end
events_controller.rb
module FrontendAPI
class EventsController < FrontendAPI::BaseController
def index
render json: []
end
end
end
spec
RSpec.describe FrontendAPI::EventsController do
describe 'GET #index' do
context 'wrong subdomain' do
before do
#request.host = 'foo.example.com'
end
it 'responds with 404' do
get :index
expect(response).to have_http_status(:not_found)
end
end
end
end
Is there some other way of doing this?
You can accomplish this by using the full URL in your tests instead of setting the host in a before block.
Try:
RSpec.describe FrontendAPI::EventsController do
describe 'GET #index' do
let(:url) { 'http://subdomain.example.com' }
let(:bad_url) { 'http://foo.example.com' }
context 'wrong subdomain' do
it 'responds with 404' do
get "#{bad_url}/route"
expect(response).to have_http_status(:not_found)
end
end
end
end
There is a similar question and answer here testing routes with subdomain constraints using rspec
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"
)
Here's the route:
namespace :admin do
get 'statistics', to: 'dashboard#statistics', as: :statistics
end
Here's the routing spec:
it 'routes to #statistics' do
expect(get: '/admin/statistics').to route_to 'admin/dashboard#statistics'
end
It passes perfectly.
However, this controller spec, that uses the above route, fails:
RSpec.describe Admin::DashboardController, :type => :controller do
let(:user){ FactoryGirl.create :user }
let(:admin){ FactoryGirl.create :admin }
describe '#statistics:' do
let(:request){ get :statisitcs }
context 'When guest;' do
before { request }
describe 'response' do
subject { response }
its(:status){ should eq 302 }
its(:content_type){ should eq 'text/html' }
it{ should redirect_to 'new' }
end
end
end
end
The problem is:
1) Admin::DashboardController#statistics: When admin; response content_type
Failure/Error: let(:request){ get :statisitcs }
ActionController::UrlGenerationError:
No route matches {:action=>"statisitcs", :controller=>"admin/dashboard"}
But doesn't the routing spec prove that such a route exists?
Looks like your request is misspelled in the controller spec?
let(:request){ get :statisitcs }
Should be
let(:request){ get :statistics }
Based on the request spec and the route definition. Its showing up as misspelled in the failed test as well, so...
I have this RSpec test:
# /spec/controllers/user/builder_tab/biographies_controller_spec.rb
describe User::BuilderTab::BiographiesController do
before(:each) do
#user = create :user
sign_in #user
end
describe "#update" do
it "can update" do
patch :update, profile: { name: "Joe Peshi" }
expect(User.first.profile.name).to eq "Joe Peshi"
end
end
end
But when I run it - for some reason it makes this request:
No route matches {:profile=>{:name=>"Joe Peshi"}, :controller=>"user/biographies", :action=>"update"}
Which obviously fails because the path is not /user/biographies - it's /user/builder_tab/biographies.
How do I fix it?
My routes are:
namespace :user do
namespace :builder_tab do
resource :biography, only: [:edit, :update]
end
end
Builder_tab is defined like so:
class User::BuilderTab < User
end
try to be verbose with your modules and see if it helps
module User
module BuilderTab
describe BiographiesController
..
end
end
end
I wrote a spec to test the instance variable #vendors in the index action of my vendors controller. If I remove #vendors from the vendors controller the spec still passes. Any ideas as to why assigns(:vendors) would pass if #vendors doesn't exists in the controller. Heres my code:
Vendors Controller
class VendorsController < ApplicationController
load_and_authorize_resource
def index
# #vendors = Vendor.all
end
end
Vendors Controller Spec
require 'spec_helper'
require 'ruby-debug'
describe VendorsController do
login_user
before(:each) do
#vendor = Factory(:vendor)
end
describe "GET index" do
before(:each) do
#ability.can :read, Vendor
end
it "assigns all vendors to #vendors" do
get :index
assigns(:vendors).should == [#vendor]
end
it "should render the index template" do
get :index
response.should be_success
response.code.should eq("200")
response.should render_template("index")
end
end
end
Vendors Factory
FactoryGirl.define do
factory :vendor do |f|
f.sequence(:name) { |n| "Test#{n}" }
f.sequence(:address) { |n| "000 Test#{n} drive, Hampton" }
f.state "Virginia"
f.zip "00000"
f.sequence(:telephone) { |n| "000-000-000#{n}" }
f.sequence(:poc) { |n| "Test#{n}" }
end
end
Thanks
Because load_and_authorize_resource actually loads and authorizes.
So your code is unnecessary.
You could change with authorize_resource, thus the spec will fail.