0

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

 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

任何指针将不胜感激!

4

0 回答 0