【发布时间】:2015-05-11 08:10:07
【问题描述】:
我正在构建一个简单的博客应用程序,以便使用 RSpec 和 Factory Girl 来学习 BDD/TDD。在这个过程中,我继续遇到“失败”,但我相信他们更多地与我如何使用工厂女孩有关。
正如您将在下面看到的那样,为了让我的规格通过,我很难保持我的测试 DRY - 我一定有什么误解。你会注意到,我没有充分利用 Factory Girl 的潜力,有时甚至完全跳过它。我发现在规范中使用 get :create、get :show 或 put :update 等函数时,我通常会遇到问题。
我目前停留在 #PUT update 规范上,该规范应该只是测试 @post 变量的分配。我已经尝试了多种我在网上找到的这种规格,但似乎没有一种工作 - 因此,它是工厂女孩吗?也许我在网上找到的规格是过时的 Rspec 版本?
我正在使用: 规范 3.1.7 导轨 4.1.6
posts_controller_spec.rb
require 'rails_helper'
require 'shoulda-matchers'
RSpec.describe PostsController, :type => :controller do
describe "#GET index" do
it 'renders the index template' do
get :index
expect(response).to be_success
end
it "assigns all posts as @posts" do
post = Post.create(title: 'Charlie boy', body: 'Bow wow wow ruff')
get :index
expect(assigns(:posts)).to eq([post])
end
end
describe '#GET show' do
it 'assigns the request post to @post' do
post = Post.create!(title: 'Charlie boy', body: 'Bow wow wow ruff')
get :show, id: post.id
expect(assigns(:post)).to eq(post)
end
end
describe '#GET create' do
context 'with valid attributes' do
before :each do
post :create, post: attributes_for(:post)
end
it 'creates the post' do
expect(Post.count).to eq(1)
expect(flash[:notice]).to eq('Your post has been saved!')
end
it 'assigns a newly created post as @post' do
expect(assigns(:post)).to be_a(Post)
expect(assigns(:post)).to be_persisted
end
it 'redirects to the "show" action for the new post' do
expect(response).to redirect_to Post.first
end
end
context 'with invalid attributes' do
before :each do
post :create, post: attributes_for(:post, title: 'ha')
end
it 'fails to create a post' do
expect(Post.count).to_not eq(1)
expect(flash[:notice]).to eq('There was an error saving your post.')
end
it 'redirects to the "new" action' do
expect(response).to redirect_to new_post_path
end
end
end
describe '#GET edit' do
it 'assigns the request post to @post' do
post = Post.create!(title: 'Charlie boy', body: 'Bow wow wow ruff')
get :edit, id: post.id
expect(assigns(:post)).to eq(post)
end
end
describe '#PUT update' do
context 'with success' do
before :each do
post :create, post: attributes_for(:post)
end
it 'assigns the post to @post' do
put :update, id: post.id
expect(assigns(:post)).to eq(post)
end
end
end
end
posts_controller.rb
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!"
redirect_to @post
else
flash[:notice] = "There was an error saving your post."
redirect_to new_post_path
end
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
# if @post.update(params[:post].permit(:title, :body))
# flash[:notice] = "Your post is updated!"
# redirect_to @post
# else
# flash[:notice] = "There was an error updating your post."
# render :edit
# end
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
factories/post.rb
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
当前故障:
Failures:
1) PostsController#PUT update with success assigns the post to @post
Failure/Error: put :update, id: post.id
ArgumentError:
wrong number of arguments (0 for 1+)
# ./spec/controllers/posts_controller_spec.rb:86:in `block (4 levels) in <top (required)>'
Finished in 0.19137 seconds (files took 1.17 seconds to load)
17 examples, 1 failure
【问题讨论】:
标签: ruby-on-rails ruby rspec factory-bot bdd