简体   繁体   中英

Factory Girl & Rspec controller test failure

I am still fairly new to testing and still wrapping my head around Factory Girl, which I believe to be the culprit of this failure. As simple as the solution will probably be, I have searched other posts with the same failure message but the answers are not working for me.

I decided to learn BDD/TDD by building this simple blog app. Here is the failure message:

Failures:

  1) PostsController POST create creates a post
     Failure/Error: expect(response).to redirect_to(post_path(post))
       Expected response to be a <redirect>, but was <200>

The test:

RSpec.describe PostsController, :type => :controller do
    let(:post) { build_stubbed(:post) }

    describe "POST create" do
        it "creates a post" do
          expect(response).to redirect_to(post_path(post))
          expect(assigns(:post).title).to eq('Kicking back')
          expect(flash[:notice]).to eq("Your post has been saved!")
        end
    end
end

My Factory Girl file:

FactoryGirl.define do
    factory :post do
        title 'First title ever'
        body 'Forage paleo aesthetic food truck. Bespoke gastropub pork belly, tattooed readymade chambray keffiyeh Truffaut ennui trust fund you probably haven\'t heard of them tousled.'
    end
end

The controller:

class PostsController < ApplicationController

    def index
        @posts = Post.all.order('created_at DESC')
    end

    def new
        @post = Post.new
    end

    def create
        @post = Post.new(post_params)

        if @post.save
            flash[:notice] = "Your post has been saved!"
        else
            flash[:notice] = "There was an error saving your post."
        end
        redirect_to @post
    end

    def show
        @post = Post.find(params[:id])
    end

    private

    def post_params
        params.require(:post).permit(:title, :body)
    end
end 

In case it's relevant, here's my Gemfile:

gem 'rails', '4.1.6'

...

group :development, :test do
  gem 'rspec-rails', '~> 3.1.0'
  gem 'factory_girl_rails', '~> 4.5.0'
  gem 'shoulda-matchers', require: false
  gem 'capybara'
end

Any help is appreciated.

Try this for your tests:

context 'with valid attributes' do
  it 'creates the post' do
    post :create, post: attributes_for(:post)
    expect(Post.count).to eq(1)
  end

  it 'redirects to the "show" action for the new post' do
    post :create, post: attributes_for(:post)
    expect(response).to redirect_to Post.first
  end
end

Personally I'd also separate some of those expects you did in to different tests. However, I don't know that testing that they're set that way is really necessary in a controller.

edit: There is also an issue with your create action where in the event of it not successfully saving it will still try to redirect to the @post which will fail. Your test with invalid attributes should highlight this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM