rspec的新手,期待<“index”>但是用<“”>渲染

[英]newbie for rspec,expecting <“index”> but rendering with <“”>


FactoryGirl.define do
  factory :card do
    card_no '6217920016000864'

    factory :invalid_card do
      card_no nil
    end
  end
end

card_controller_spec.rb

card_controller_spec.rb

describe CardsController do
  describe 'GET #index' do
    it 'assigns card' do
      card = create(:card)
      get :index
      expect(assigns(:card)).to eq([card])
    end

    it 'show index' do
      expect(response).to render_template("index")
    end
  end
end

cards_controller.rb

cards_controller.rb

class CardsController < ApplicationController
  def index
    if current_user.login_name=="admin"
      #admin
      @cardlist = set_paginate Card
    else
      #普通管理員
      @restaurant_ids = Restaurant.where('parent_id = ? or id = ?', current_user.restaurant.id, current_user.restaurant.id).collect { |r| r.id }
      @cardlist = set_paginate Card.where('restaurant_id in (?) ', @restaurant_ids)
    end
  end
end

two errors like this expecting <"index"> but rendering with <""> and expect(assigns(:card)).to eq([card]) got nil.

像這樣期待<“index”>的兩個錯誤,但用<“”>和期望(分配(:卡))。到eq([card])的渲染得到了nil。

help me thanks!

幫幫我謝謝!

2 个解决方案

#1


2  

First test is for what data controller provides to the view. Looking on the controller you have you should check for assigns(:cartdlist) I think. And use match_array rather than eq.

第一個測試是針對視圖提供的數據控制器。在控制器上看,你應該檢查一下(:cartdlist)我認為。並使用match_array而不是eq。

Second test doesn't render anything as it doesn't go anywhere (your get is just for the first it block). So try this:

第二次測試沒有渲染任何東西,因為它沒有去任何地方(你的獲取只是第一次阻止)。所以試試這個:

describe CardsController do
  describe 'GET #index' do
    before do
      card = create(:card)
      get :index
    end

    it 'assigns card' do
      expect(assigns(:cardlist)).to match_array([card])
    end

    it 'show index' do
      expect(response).to render_template("index")
    end
  end
end

It's probably not the problem in your case as you got different error but to allow expect syntax your spec_helper.rb file should contain:

這可能不是你的情況下的問題,因為你有不同的錯誤,但允許你的spec_helper.rb文件應該包含的期望語法:

RSpec.configure do |config|
  # most omitted ... 
  config.expect_with :rspec do |c|
    c.syntax = :expect
  end
end

or c.syntax = [:should, :expect] for both

或者c.syntax = [:should,:expect]

#2


1  

This

這個

expect(assigns(:card)).to eq([card]) got nil

because I believe you assign data to cards (after updating question: cardlist)

因為我相信你把數據分配給卡片(更新問題后:卡片表)

This

這個

expecting <"index"> but rendering with <"">

because you don't call get :index in test

因為你沒有在測試中調用get:index


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/04/01/7258d4bcaa43bb69a6baef60051c20ff.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com