Ruby on rails 带rspec的设计由于之前的测试而失败

Ruby on rails 带rspec的设计由于之前的测试而失败,ruby-on-rails,rspec,devise,rspec2,Ruby On Rails,Rspec,Devise,Rspec2,我在rspec测试中使用了Desive。这是我的测试 describe BooksController do before(:all) do @user = FactoryGirl.create(:user) end describe "GET index" do it "shows list of current user books" do sign_

我在rspec测试中使用了Desive。这是我的测试

    describe BooksController do
        before(:all) do
            @user = FactoryGirl.create(:user)  
          end

        describe "GET index" do
            it "shows list of current user books" do
              sign_in @user
              book = @user.books.create!(:title => "user")
              get :index, {}
              assigns(:books).should eq(@user.books)
            end
          end

          describe "GET show" do
            it "assigns the requested book as @book" do
              sign_in @user
              book = @user.books.create!(:title => "user")
              visit_count = book.visits.to_i
              get :show, {:id => book.to_param}
              assigns(:book).should eq(book)
              book = Book.find(book.id)
              visit_count.should_not eq(book.visits)
            end
          end

          describe "GET new" do
            it "assigns a new book as @book" do
              sign_in @user
              get :new, {}
              assigns(:book).should be_a_new(Book)
            end
    end

end
工厂

FactoryGirl.define do
  factory :user do
    sequence(:email) { |n| "foo#{n}@example.com" }
    password '12345678'
    password_confirmation '12345678'
    confirmed_at Time.now
  end
end
图书管理员

class BooksController < ApplicationController
  before_action :authenticate_user!, only: [:index, :edit, :update, :destroy, :new, :my_books, :add_wish_list]

  # GET /books
  # GET /books.json
  def index
    @books = current_user.books
  end

  # GET /books/1
  # GET /books/1.json
  def show
    @book = Book.find(params[:id])
    @book.book_visit_count
    if(session["warden.user.user.key"].present?)
      @book.book_visit_user(session["warden.user.user.key"][0][0])
    end
  end

  # GET /books/new
  def new 
    @book = Book.new
  end

end
class BooksController
错误

失败/错误:分配(:book)。应该是新的(book)
期望零是一本新书(id:integer,title:string,author:string,isbn\u 10:string,isbn\u 13:string,edition:integer,print:integer,publication\u year:integer,publication\u month:string,condition:string,value:integer,status:boolean,stage:integer,description:text,visions:integer,user\u id:integer,prefered\u place:string,prefered\u time:string,created\u:datetime,更新时间:datetime,评级:整数,图像:字符串,发布者:字符串,goodreads\u id:string)
#./spec/controllers/books\u controller\u spec.rb:66:in'block(3层)in'
问题是,当我作为一个整体运行测试时,第三个测试“get new”失败,但当我单独运行它时通过。而且,如果我删除控制器中的before_authenticate!之前的测试,则所有测试都通过。 同样,如果我注释掉前两个描述块中的“赋值”,那么所有测试都会再次通过。
我使用的是rails 4.0.2和rspec 2.14.7,Desive 3.2.2

我唯一能想到的是,你的
authenticate\u user
方法对于以前经过身份验证的用户是失败的。它不会影响
show
,因为你没有
:show
列在你的
动作之前,你可以测试这个理论通过要求对
show
进行身份验证,并查看您的第二个示例在(:all)
之前是否也失败。

我对rspec很陌生,但(:all)
之前是否意味着您对所有测试都使用共享对象?可能将其更改为(:each)之前(:each)(较慢)或在(:each)之后添加一些
块,您将在其中清除属性?您只是在显示测试的一个片段吗?之前的
块之外使用时,应生成
未定义的方法
错误,如您所做的那样描述
块。是的,这是一个片段。我将在(:each)工作之前用完整的代码更新。但是为什么它在之前不工作(:all)。共享用户对象时是否有任何错误,这在所有用户中都很常见。您的规范中仍然缺少一个
end
。是否复制/粘贴了整个文件?谢谢!是的,这是sing_in的问题。因此,在为Desive user设置工厂时,我们不应使用before(:all)。
 Failure/Error: assigns(:book).should be_a_new(Book)
       expected nil to be a new Book(id: integer, title: string, author: string, isbn_10: string, isbn_13: string, edition: integer, print: integer, publication_year: integer, publication_month: string, condition: string, value: integer, status: boolean, stage: integer, description: text, visits: integer, user_id: integer, prefered_place: string, prefered_time: string, created_at: datetime, updated_at: datetime, rating: integer, image: string, publisher: string, goodreads_id: string)
     # ./spec/controllers/books_controller_spec.rb:66:in `block (3 levels) in <top (required)>'