RSpec中的Click_button导致“缺少所需的密钥:[:id]'

时间:2015-07-26 11:05:31

标签: ruby-on-rails ruby-on-rails-4 rspec capybara

这是我的spec文件,我想测试添加玩家:

add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
    $content = "If you like this post, then please consider retweeting it or sharing it on Facebook.";
    return $content;
}

当我进行测试时,我得到:

RSpec.describe "AddPlayers", type: :request do
  describe "add player" do
    let(:player) { FactoryGirl.create :player }
    it "add new player with valid data" do
      visit new_player_path
      fill_in "player_firstname", with: player.firstname
      fill_in "player_lastname", with: player.lastname
      fill_in "player_nickname", with: player.nickname

      click_button "Add player"
    end
  end
end

我名为 Failure/Error: click_button "Add player" ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"players", :id=>nil} missing required keys: [:id] 的FactoryGirls如下所示:

players.rb

我的控制器看起来像这样:

FactoryGirl.define do
  factory :player do
    firstname "John"
    lastname "Smith"
    nickname "smithy99"
    avatar { File.new(Rails.root.join('spec/images/1.png')) }
  end
end

我的 def new @player = Player.new end def create @player = Player.create(player_params) redirect_to player_path(@player) end 型号:

Player

当我进入浏览器链接时:

  

http://localhost:3000/players/new

然后点击"添加播放器"如果没有归档输入字段,我会得到与RSpec相同的错误:

  

没有路线匹配{:action =>" show",:controller =>" player",:id => nil}   缺少必需的键:[:id]

所以我的问题是我的规范有什么问题以及如何处理它?为什么FactoryGirl创建的class Player < ActiveRecord::Base validates :nickname, presence: true, uniqueness: true; validates :firstname, :lastname, presence: true; has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" } validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ def full_name "#{firstname} #{lastname} (#{nickname})" end # other custom methods... end 没有player

0 个答案:

没有答案