【问题标题】:Factory Girl & Rspec controller test failureFactory Girl & Rspec 控制器测试失败
【发布时间】:2015-03-08 20:51:03
【问题描述】:

我对测试还很陌生,并且仍然围绕着工厂女孩,我认为这是这次失败的罪魁祸首。尽管解决方案可能很简单,但我已经搜索了具有相同失败消息的其他帖子,但答案对我不起作用。

我决定通过构建这个简单的博客应用来学习 BDD/TDD。以下是失败信息:

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>

测试:

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

我的工厂女孩​​档案:

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

控制器:

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 

如果相关,这是我的 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

感谢任何帮助。

【问题讨论】:

    标签: ruby-on-rails rspec tdd factory-bot rspec3


    【解决方案1】:

    试试这个进行测试:

    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
    

    就我个人而言,我也会将您在不同测试中所做的一些期望分开。但是,我不知道在控制器中测试它们是否以这种方式设置是否真的有必要。

    编辑: 您的创建操作还有一个问题,如果它没有成功保存,它仍然会尝试重定向到 @post 这将失败。您的无效属性测试应该突出显示这一点。

    【讨论】:

    • 感谢您的回复!我绝对同意那些期望看起来更好分开。我试过了,第一个规范通过了,但第二个仍然失败:Failure/Error: post :create, event: attributes_for(:post) ActionController::ParameterMissing: param is missing or the value is empty: post
    • 糟糕 - 更新了我的答案...复制并粘贴受害者 :^)
    • 没问题 - 我还在回答中添加了有关您的创建操作的更多信息
    • 好电话!我将更改 if/else 以适应这种情况。
    • 无论如何你都会在你的测试中接受它(假设你测试了它们是否有无效的属性)。继续测试;这是值得的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多