【问题标题】:rspec BDD tests - trying to test index controller methodrspec BDD 测试 - 尝试测试索引控制器方法
【发布时间】:2019-06-21 21:56:44
【问题描述】:

我有一个带有“模型”属性的汽车模型的 rails 应用程序。我只是想运行一个测试,看看汽车控制器中的索引方法是否会显示所有汽车。 我不知道该怎么做。测试当前通过了,但它不应该因为我目前有一个空索引方法。我该如何测试这个以及我需要添加什么。我试图阅读文档但没有运气。谢谢

require 'rails_helper'

RSpec.describe CarsController, type: :controller do

  context "test" do
    it "displays all cars" do
      get :index
    end
  end

end

【问题讨论】:

  • @MarkMerritt 对于要显示的每个车型
  • @MarkMerritt 我明白发生了什么,但我不知道从那里拿它,因此问题,但感谢您的回答。
  • 我推荐 RSpec Controller Spec Documentation 作为开始学习测试的好地方。
  • @Tom 谢谢你的建议。我用过这个,它似乎工作。但是我的终端说我需要添加gem 'rails-controller-testing' 才能工作。但是,除非我错过了,否则我在网站上的任何地方都没有看到它。怎么gem 'rspec-rails'不够用?
  • @b.herring 如果你想使用assigns,你还需要使用rails-controller-testing gem。这是 RSpec 团队做出的结构性决定。把它放在你的 Gemfile 中,你会很高兴的

标签: ruby-on-rails ruby rspec tdd bdd


【解决方案1】:

使用您发布的当前测试文件,您的测试应该通过,因为您正在对索引操作发出请求,并且只要有索引操作,测试就会通过。

require 'rails_helper'

RSpec.describe CarsController, type: :controller do

  context "test" do
    it "displays all cars" do
      get :index
    end
  end
end

但是我看到有些人建议使用assigns,当使用assigns 进行测试时,您需要将所有汽车分配给一个实例变量,例如@cars = Cars.all 或您需要的任何记录,并且您应该正确设置测试数据。

在您的测试中,您还应该有一些要测试的汽车列表,例如假设您使用的是 FactoryGirl/Bot: let(:cars) { create_list :car, 3 }

你的测试文件应该看起来像这样:

require 'rails_helper'

RSpec.describe CarsController, type: :controller do
  let(:cars) { create_list :car, 3 }

  context "test" do
    it "displays all cars" do
      get :index
      expect(assigns(:cars)).to eq cars
    end
  end
end

希望我的回答有帮助。

【讨论】:

    【解决方案2】:

    您正在寻找控制器规格中的use assigns。由于您似乎还没有任何代码,因此向您展示示例的最快方法是运行rails g scaffold car。假设您的 Gemfile 中有 rspec-rails,您应该在 spec/controllers/cars_controller_spec.rb 中得到类似的内容:

    require 'rails_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.
    
    RSpec.describe CarsController, type: :controller do
    
      # This should return the minimal set of attributes required to create a valid
      # Car. As you add validations to Car, be sure to
      # adjust the attributes here as well.
      let(:valid_attributes) {
        skip("Add a hash of attributes valid for your model")
      }
    
      let(:invalid_attributes) {
        skip("Add a hash of attributes invalid for your model")
      }
    
      # 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
      # CarsController. Be sure to keep this updated too.
      let(:valid_session) { {} }
    
      describe "GET #index" do
        it "assigns all cars as @cars" do
          car = Car.create! valid_attributes
          get :index, params: {}, session: valid_session
          expect(assigns(:cars)).to eq([car])
        end
      end
    
      describe "GET #show" do
        it "assigns the requested car as @car" do
          car = Car.create! valid_attributes
          get :show, params: {id: car.to_param}, session: valid_session
          expect(assigns(:car)).to eq(car)
        end
      end
    
      describe "GET #new" do
        it "assigns a new car as @car" do
          get :new, params: {}, session: valid_session
          expect(assigns(:car)).to be_a_new(Car)
        end
      end
    
      describe "GET #edit" do
        it "assigns the requested car as @car" do
          car = Car.create! valid_attributes
          get :edit, params: {id: car.to_param}, session: valid_session
          expect(assigns(:car)).to eq(car)
        end
      end
    
      describe "POST #create" do
        context "with valid params" do
          it "creates a new Car" do
            expect {
              post :create, params: {car: valid_attributes}, session: valid_session
            }.to change(Car, :count).by(1)
          end
    
          it "assigns a newly created car as @car" do
            post :create, params: {car: valid_attributes}, session: valid_session
            expect(assigns(:car)).to be_a(Car)
            expect(assigns(:car)).to be_persisted
          end
    
          it "redirects to the created car" do
            post :create, params: {car: valid_attributes}, session: valid_session
            expect(response).to redirect_to(Car.last)
          end
        end
    
        context "with invalid params" do
          it "assigns a newly created but unsaved car as @car" do
            post :create, params: {car: invalid_attributes}, session: valid_session
            expect(assigns(:car)).to be_a_new(Car)
          end
    
          it "re-renders the 'new' template" do
            post :create, params: {car: invalid_attributes}, session: valid_session
            expect(response).to render_template("new")
          end
        end
      end
    
      describe "PUT #update" do
        context "with valid params" do
          let(:new_attributes) {
            skip("Add a hash of attributes valid for your model")
          }
    
          it "updates the requested car" do
            car = Car.create! valid_attributes
            put :update, params: {id: car.to_param, car: new_attributes}, session: valid_session
            car.reload
            skip("Add assertions for updated state")
          end
    
          it "assigns the requested car as @car" do
            car = Car.create! valid_attributes
            put :update, params: {id: car.to_param, car: valid_attributes}, session: valid_session
            expect(assigns(:car)).to eq(car)
          end
    
          it "redirects to the car" do
            car = Car.create! valid_attributes
            put :update, params: {id: car.to_param, car: valid_attributes}, session: valid_session
            expect(response).to redirect_to(car)
          end
        end
    
        context "with invalid params" do
          it "assigns the car as @car" do
            car = Car.create! valid_attributes
            put :update, params: {id: car.to_param, car: invalid_attributes}, session: valid_session
            expect(assigns(:car)).to eq(car)
          end
    
          it "re-renders the 'edit' template" do
            car = Car.create! valid_attributes
            put :update, params: {id: car.to_param, car: invalid_attributes}, session: valid_session
            expect(response).to render_template("edit")
          end
        end
      end
    
      describe "DELETE #destroy" do
        it "destroys the requested car" do
          car = Car.create! valid_attributes
          expect {
            delete :destroy, params: {id: car.to_param}, session: valid_session
          }.to change(Car, :count).by(-1)
        end
    
        it "redirects to the cars list" do
          car = Car.create! valid_attributes
          delete :destroy, params: {id: car.to_param}, session: valid_session
          expect(response).to redirect_to(cars_url)
        end
      end
    
    end
    

    【讨论】:

    • 否决?我试图提供帮助。为什么投反对票?使用脚手架生成器没有任何问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多