为什么我的rspec测试失败了ActionController :: RoutingError:?

时间:2012-07-18 20:28:45

标签: ruby-on-rails rspec capybara

基本上我很难理解为什么这个特定的测试失败了。当我进入并手动测试时,页面工作正常,但测试仍然失败。首先,这是错误。

 2) Setlist Pages Edit page adding a song 
     Failure/Error: click_button submit
     ActionController::RoutingError:
       No route matches {:action=>"edit", :controller=>"setlists", :id=>nil}
     # ./app/controllers/allocations_controller.rb:15:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/setlist_pages_spec.rb:79:in `block (4 levels) in <top (required)>'

我意识到id设置为nil但我知道当测试最初访问它呈现的页面时,因为内容测试通过。在我测试编辑操作时,我不确定它为什么指向创建控制器?测试如下所示:

before do
   @setlist = Setlist.create(date: Date.today, morning: true)
end 

...

describe "Edit page" do

        let(:admin) { FactoryGirl.create(:admin) }
        let(:submit){ "Add Song" }

        before do
           @secondSong = FactoryGirl.create(:song)
           sign_in admin
           visit edit_setlist_path(@setlist)
        end

        it{ should have_content("Edit a Setlist")}

        describe "adding a song" do
            before do
                select("#{@secondSong.title} by #{@secondSong.artist}", from: 'Songs')
                click_button submit

            end

            it{ should have_selector('div.alert.alert-success')}

            it "should create a new allocation" do
                expect{click_button submit}.to change(Allocation, :count).by(1)
            end

        end # end adding a song
    end # end edit test

请求的控制器代码:

def create
    @setlist = Setlist.new(params[:setlist])
    if @setlist.save
        #success!
        flash[:success] = "Setlist saved"
      #@setlist.allocations.build produce invalid allocation with nil id
        redirect_to setlist_path(@setlist)
    else
        #FAIL!
        render 'new'
    end
  end
  def edit
    @songs= Song.search(params[:search])
    #@songs = Song.all(order: 'title')
    @setlist = Setlist.find(params[:id])
    @allocations = @setlist.allocations
    @allocation = Allocation.new
    @selections = Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id ]}
  end


  def update

    @setlist = Setlist.find(params[:id])
    @selections = Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id] }
    @allocations = @setlist.allocations
    @allocation = Allocation.new

    #Allocation parameters
    @allocation.song_id = params[:allocation][:song_id]
    @allocation.setlist_id = @setlist.id
    @allocation.songPosition = @setlist.songs.count + 1

    if @setlist.update_attributes(params[:setlist])
      if @allocation.save
        flash[:success] = "SETLIST SAVED!"
        redirect_to edit_setlist_path(@setlist)
      else
        flash[:fail] = "Sorry there was an error adding songs to the setlist"
        render 'edit'
      end
    else
      flash[:fail] = "Invalid Set"
      render 'edit'
    end
  end

任何指针都会非常感激!

0 个答案:

没有答案