logo
down
shadow

RSPEC test index action with before_action filter


RSPEC test index action with before_action filter

By : kevin
Date : November 19 2020, 03:59 PM
I wish this helpful for you After long discussion I think tests should be smth like this (it is not tested :) )
code :
require 'rails_helper'

RSpec.describe ActivitiesController, :type => :controller do

  describe "GET index" do

    before(:each) do
      @activity = FactoryGirl.create(:activity)
    end

    context 'when user is logged' do

      before(:each) do
        session[:current_user] = @activity.user_id
      end

      it "shows all activities for signed in user" do
        get :index, {user_id: @activity.user_id}
        expect(response).to be_success      
      end  
    end

    context 'when user is anonymous' do
      it "redirects user to root path" do
        get :index, {user_id: @activity.user_id}
        expect(response).to redirect_to root_path
      end  
    end

  end


end


Share : facebook icon twitter icon
Rspec controller test, trying to create a test for a 'processlogin' action

Rspec controller test, trying to create a test for a 'processlogin' action


By : user3091431
Date : March 29 2020, 07:55 AM
Hope this helps Aah, for the error message. I'm assuming that line 11 is is_login_valid(params[:user][:user_name], params[:user][:password]).
As you're not sending any params in your test post params[:user] is nil hence the nil.[] error (when the controller is looking for params[:user][:user_name]), you set params by passing them as a hash as the 2nd parameter to post in your test.
Spree action require_login creates a redirect loop when assigned to the before_action filter

Spree action require_login creates a redirect loop when assigned to the before_action filter


By : rashanimm
Date : March 29 2020, 07:55 AM
I hope this helps you . I was able to get this to work without rendering a custom page by changing the placement of skip_before_action. According to the rails documentation that you linked to:
code :
class LoginsController < ApplicationController   
   skip_before_action :require_login, only: [:new, :create] 
end
class ApplicationController < ActionController::Base
  before_action :require_login

  private

  def logged_in?
    spree_current_user != nil
  end

  def require_login
    unless logged_in?
      flash[:error] = "Please Login or Sign Up"
      redirect_to spree_login_path
    end
  end
end
Spree::UserSessionsController.class_eval do
  skip_before_action :require_login

end
RSpec controller for nested resources, before_action

RSpec controller for nested resources, before_action


By : Jennie Garcia
Date : March 29 2020, 07:55 AM
Any of those help Your route will look like this: graves/:grave_id/candles, so you have to pass a @grave.id to your post method as grave_id.
code :
post :create, grave_id: @grave.id, candle: { name: "Dummy name", body: "Dummy body", grave_id: @grave.id}
How to show flash message of before_action filter authenticate_user! on a action which is called through ajax

How to show flash message of before_action filter authenticate_user! on a action which is called through ajax


By : Dave
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You can call flash message inside your js.erb file which you are calling when the user clicks on the vote.
vote.js.erb
code :
<%= flash[:notice] %>
respond_to do |format|
  if @vote.valid?
    format.html { flash[:notice] = 'Thanks for voting.' }
    format.js   { flash[:notice] = 'Thanks for voting.' }
  else
    format.html { flash[:error] = 'You can only vote on a post once.'}
    format.js   { flash[:error] = 'You can only vote on a post once.'}
  end
end
def authenticate_user!
  unless current_user
    render 'yourjs'
  end
end
RSpec test controller action create not affect test database

RSpec test controller action create not affect test database


By : mofajjul
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have controller Categories which have action create: , It's because you have this line:
code :
config.use_transactional_fixtures = true
Related Posts Related Posts :
  • Why "bundle install" try to install outdated version of gems?
  • Rails - Customize model validation error messages but without a database
  • When to switch from cucumber to rspec in the BDD cycle for a login procedure
  • Multipart response for web service
  • How to hide Add new option in Rails Admin
  • Override CollectionProxy find method
  • Rails 4 simple_form has_many through check boxes not saving
  • ruby on rails authentication using devise gem
  • Rails iteration over arrays and hashes
  • Creating inline date_select dropdowns using simple_form and zurb foundation
  • NameError in SongsController#index uninitialized constant Song::FriendlyId
  • Rails how to improve if record exists?
  • omniauth custom request phase form
  • Rails 5 - Saving the absolute current url via a button to a cookie?
  • How can / should I override the `build` method for a my model?
  • Install Rails Failed on OSX 10.8.4
  • ruby on rails - undefined method valid?
  • Users Registration on POST
  • Rails application variables in carrierwave store_dir
  • How to customize simple_form for json nested hash
  • Couldn't find file 'jquery', Sprockets::FileNotFound in StaticPages#home error
  • How should I use the alias_method_chain for the build method?
  • how to create two custom registration forms in devise?
  • Rails 4 link_to Destroy not working in Getting Started tutorial
  • How does ruby on rails dynamically define `find_by_id`, `find_by_name` methods?
  • JBuilder dynamic keys for model attributes
  • `initialize': No such file or directory - getaddrinfo (Errno::ENOENT) when Rails new app
  • What is the Ruby equivalent of Node.js' socket.io?
  • Where rails store standard helpers?
  • how to check expected result matches any of give values in rspec?
  • Form submission to create multiple objects
  • Simple Twitter Login with Rails and Email confirmation
  • Where should helpful functions for my controller go in Rails?
  • Using session variable in the Model or the other way
  • Why can't I have ruby code and comments in Embedded Ruby?
  • Rails: How can I make an object available in all views?
  • Cannot enter simply form information into SQLite DB (Rails)
  • Rails - Retain form values on failed submission. Getting "First argument in form cannot contain nil or be empty&quo
  • How to handle STI and build related models automatically?
  • Suppress an error when saving a record in Rails
  • Rails AJAX reload the page. Why?
  • How to pass user attributes to action mailer
  • Custom Validator to Prevent an appointment from being scheduled too early in day?
  • Postgres Enable TCP/IP Connection
  • How do I avoid the circular argument reference warning in activesupport
  • Unable to push to heroku master because of rails/spring version error
  • deep nested routes in rails
  • Best approach to Users Profile Images in Rails App
  • RailsInstaller: Can I maintain the gem fold when re-installing a new version?
  • couldn't find file 'jquery-ui' i get this error even after having this file in my application.js
  • Ruby regex to split user and date text input
  • Tool like BetterErrors for JRuby?
  • redirect_to in action destroy ruby on rails
  • Where are the I18n.t link and reference paths?
  • Sending emails to multiple recipients in rails app
  • Rails, Devise, Sign up = write name in other Database
  • pass custom attribute on rspec factory_girl controller test
  • rails generate uses wrong version of Ruby
  • Duplicate Requests from a Browser
  • One action in Rails controller sends no data back
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org