1

応答は<:redirect>であると予想されましたが、<200>でした

私のテストは:

describe "Link POST #create" do

  context "with valid attributes" do
    it "creates a new link" do
      expect{
        post :create, link: FactoryGirl.create(:link, :group => @group)
      }.to change(Link,:count).by(1)
    end

    it "redirects to the new link" do
      post :create, link: FactoryGirl.create(:link, :group => @group)
      # response.should redirect_to @link # Link.unscoped.last
      response.should redirect_to Link.unscoped.last # render_template :show
    end
  end

最初のテストは成功しますが、2番目のテストは失敗します。

私のコードは次のとおりです。

  def create
    @link = Link.new(params[:link])

    respond_to do |format|
      if @link.save
        flash[:notice] = 'Link was successfully created.'
        format.html { redirect_to(@link) }
        format.xml  { render :xml => @link, :status => :created, :location => @link }
      else
        @selected_group = params[:group_id]
        format.html { render :action => "new" }
        format.xml  { render :xml => @link.errors, :status => :unprocessable_entity }
      end
    end
  end

リダイレクトとレンダリングを試しましたが、2番目のテストに合格できません。

4

1 に答える 1

1

これが機能するはずの何かです:

describe "Link POST #create" do

  context "with valid attributes" do

    def do_post( format = 'html' )
      attributes = FactoryGirl.build(:link).attributes.merge( :group_id => @group.id )
      post :create, :link => attributes, :format => 'html'
    end

    it "creates a new link" do
      expect{
        do_post
      }.to change(Link,:count).by(1)
    end

    it "redirects to the new link" do
      do_post
      response.should redirect_to( assigns[:link] )
    end
  end

最初の仕様は、 FactoryGirl.createを呼び出していたためにのみ機能していたため、コントローラーからレコードが作成されていましたが、コントローラーの呼び出しが機能していなかった可能性があります。

于 2012-06-10T04:02:57.387 に答える