Jump to content

The ultimate community for Ruby on Rails developers.


Photo

NameError: uninitialized constant Crud

rspec ruby-on-rails-4

  • Please log in to reply
2 replies to this topic

#1 dailammoc

dailammoc

    Dispatcher

  • Members
  • 38 posts
  • Location/

Posted 16 April 2014 - 07:41 AM

I've written a rspec test for my controller file. Here is my controller code:

class CrudsController < ApplicationController
  before_action :load_crud
  before_action :set_crud, only: [:show, :edit, :update, :destroy]

  def load_crud

  end

  # GET /cruds
  # GET /cruds.json
  def index
    @items_grid = initialize_grid(@model)
    
    @csv = CSV.generate() do |csv|
      csv << @model.column_names
      @model.all.each do |item|
        csv << item.attributes.values_at(*@model.column_names).map{|i| i.to_s.encode("cp932", "UTF-8")}
      end
    end

    @crud = @model.order(:name)
    respond_to do |format|
      format.html { render template: 'cruds/index'}
      format.csv { send_data @csv }
    end

    # render template: 'cruds/index'
  end

  # GET /cruds/1
  # GET /cruds/1.json
  def show
    render template: 'cruds/show'
  end

  # GET /cruds/new
  def new
    @crud = @model.new
    render template: 'cruds/new'
  end

  # GET /cruds/1/edit
  def edit
    render template: 'cruds/edit'
  end

  # POST /cruds
  # POST /cruds.json
  def create
    @crud = @model.new(params[:crud])

    respond_to do |format|
      if @crud.save
        format.html { redirect_to [:master, @crud], notice: "Crud was successfully created. #{undo_link}" }
        format.json { render action: 'show', status: :created, location: @crud }
      else
        format.html { render action: 'new' }
        format.json { render json: @crud.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /cruds/1
  # PATCH/PUT /cruds/1.json
  def update
    respond_to do |format|
      if @crud.update(crud_params)
        format.html { redirect_to [:master, @crud], notice: "Crud was successfully updated. #{undo_link}"}
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @crud.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /cruds/1
  # DELETE /cruds/1.json
  def destroy
    @crud.destroy
    respond_to do |format|
      format.html { redirect_to action: :index }
      format.json { head :no_content }
    end
  end
    
  def Hotel.import(file)
    
   CSV.foreach(file.path, headers: true) do |row|
    product = find_by_id(row["id"]) || new
    product.attributes = row.to_hash
    product.save
   end
  end
  
  def import
    Hotel.import(params[:file])
    redirect_to root_url, notice: "Product was successfully Imported."
  end
  

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_crud
      @crud = @model.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def crud_params
      params[@hash].permit(@model.attribute_names)
    end

    def undo_link
      view_context.link_to("undo", revert_version_path(@crud.versions.scoped.last), :method => :post)
    end
end

and here is my test:

require 'spec_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator.  If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails.  There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec.  Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.

describe CrudsController do

  # This should return the minimal set of attributes required to create a valid
  # Crud. As you add validations to Crud, be sure to
  # adjust the attributes here as well.
  let(:valid_attributes) { {  } }

  # This should return the minimal set of values that should be in the session
  # in order to pass any filters (e.g. authentication) defined in
  # CrudsController. Be sure to keep this updated too.
  let(:valid_session) { {} }

  describe "GET index" do
    it "assigns all cruds as @cruds" do
      crud = Crud.create! valid_attributes
      get :index, {}, valid_session
      assigns(:cruds).should eq([crud])
    end
  end

  describe "GET show" do
    it "assigns the requested crud as @crud" do
      crud = Crud.create! valid_attributes
      get :show, {:id => crud.to_param}, valid_session
      assigns(:crud).should eq(crud)
    end
  end

  describe "GET new" do
    it "assigns a new crud as @crud" do
      get :new, {}, valid_session
      assigns(:crud).should be_a_new(Crud)
    end
  end

  describe "GET edit" do
    it "assigns the requested crud as @crud" do
      crud = Crud.create! valid_attributes
      get :edit, {:id => crud.to_param}, valid_session
      assigns(:crud).should eq(crud)
    end
  end

  describe "POST create" do
    describe "with valid params" do
      it "creates a new Crud" do
        expect {
          post :create, {:crud => valid_attributes}, valid_session
        }.to change(Crud, :count).by(1)
      end

      it "assigns a newly created crud as @crud" do
        post :create, {:crud => valid_attributes}, valid_session
        assigns(:crud).should be_a(Crud)
        assigns(:crud).should be_persisted
      end

      it "redirects to the created crud" do
        post :create, {:crud => valid_attributes}, valid_session
        response.should redirect_to(Crud.last)
      end
    end

    describe "with invalid params" do
      it "assigns a newly created but unsaved crud as @crud" do
        # Trigger the behavior that occurs when invalid params are submitted
        Crud.any_instance.stub(:save).and_return(false)
        post :create, {:crud => {  }}, valid_session
        assigns(:crud).should be_a_new(Crud)
      end

      it "re-renders the 'new' template" do
        # Trigger the behavior that occurs when invalid params are submitted
        Crud.any_instance.stub(:save).and_return(false)
        post :create, {:crud => {  }}, valid_session
        response.should render_template("new")
      end
    end
  end

  describe "PUT update" do
    describe "with valid params" do
      it "updates the requested crud" do
        crud = Crud.create! valid_attributes
        # Assuming there are no other cruds in the database, this
        # specifies that the Crud created on the previous line
        # receives the :update_attributes message with whatever params are
        # submitted in the request.
        Crud.any_instance.should_receive(:update).with({ "these" => "params" })
        put :update, {:id => crud.to_param, :crud => { "these" => "params" }}, valid_session
      end

      it "assigns the requested crud as @crud" do
        crud = Crud.create! valid_attributes
        put :update, {:id => crud.to_param, :crud => valid_attributes}, valid_session
        assigns(:crud).should eq(crud)
      end

      it "redirects to the crud" do
        crud = Crud.create! valid_attributes
        put :update, {:id => crud.to_param, :crud => valid_attributes}, valid_session
        response.should redirect_to(crud)
      end
    end

    describe "with invalid params" do
      it "assigns the crud as @crud" do
        crud = Crud.create! valid_attributes
        # Trigger the behavior that occurs when invalid params are submitted
        Crud.any_instance.stub(:save).and_return(false)
        put :update, {:id => crud.to_param, :crud => {  }}, valid_session
        assigns(:crud).should eq(crud)
      end

      it "re-renders the 'edit' template" do
        crud = Crud.create! valid_attributes
        # Trigger the behavior that occurs when invalid params are submitted
        Crud.any_instance.stub(:save).and_return(false)
        put :update, {:id => crud.to_param, :crud => {  }}, valid_session
        response.should render_template("edit")
      end
    end
  end
  
  describe "POST import" do
  
    describe "handling CSV files" do
      describe "to import products" do
  
        before(:each) do
          @import = Crud.create! valid_attributes
          @import.CSV.stub(:foreach).with("file_path", headers: true).and_return(data)
        end
  
        def do_save
          @import.save
        end
  
        it "should process the csv file after save" do
          @import.should_receive(:process_csv)
          do_save
        end
  
        it "should load the csv file" do
          CSV.should_receive(:foreach)
          do_save
        end
      end
    end   
  end
    
  describe "DELETE destroy" do
    it "destroys the requested crud" do
      crud = Cruds.create! valid_attributes
      expect {
        delete :destroy, {:id => crud.to_param}, valid_session
      }.to change(Crud, :count).by(-1)
    end

    it "redirects to the cruds list" do
      crud = Cruds.create! valid_attributes
      delete :destroy, {:id => crud.to_param}, valid_session
      response.should redirect_to(cruds_url)
    end
  end

end

when I run test command, I got the error:

NameError:
       uninitialized constant Crud

I didn't know why and where it happended, please show me my mistake.



#2 stevieing

stevieing

    Dispatcher

  • Members
  • 40 posts

Posted 16 April 2014 - 07:45 AM

It is looking for a model called Crud. it is telling you that model does not exist.

 

You need to create a model called Crud.

 

Regards

 

Steve.



#3 dailammoc

dailammoc

    Dispatcher

  • Members
  • 38 posts
  • Location/

Posted 16 April 2014 - 08:19 AM

It is looking for a model called Crud. it is telling you that model does not exist.

 

You need to create a model called Crud.

 

Regards

 

Steve.

yeah, thank you for help.







Also tagged with one or more of these keywords: rspec, ruby-on-rails-4

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users