2014-01-12 76 views
0

我已经在rspec测试中使用设计。这是我的测试设计与rspec测试由于prevoius测试失败

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 

错误

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)>' 

当我运行的问题是第三次试验 “得到新的” 失败作为一个整体的测试,但通过时,红外单独取消。而且如果我删除了before_authenticate!然后在控制器中进行所有测试。 同样,如果我在前两个描述块中注释掉“分配”,则所有测试都会再次通过。 我正在使用rails 4.0.2和rspec 2.14.7,devise 3.2.2

+2

我对rspec很陌生,但不是'之前(:全部)'是否意味着你使用共享对象进行所有测试?也许改成'之前(:每个)'(较慢)或添加一些'之后(:每个)'块你会清除属性? – zrl3dx

+0

你只是显示了你的测试片段? ''before''应该像在以前一样在'describe'块之外使用时产生'undefined method'错误。 –

+0

是它的一个片段。我会在完整代码 – surendar

回答

1

我唯一能想到的是,您的authenticate_user方法对于先前已通过身份验证的用户而言是失败的。它不会影响show,因为您的before_action中没有列出:show。您也可以通过要求对show进行验证来验证此理论,并查看第二个示例是否也开始失败before(:all)

+0

谢谢!是的,它是一个sing_in的问题。所以我们在为设计用户设置工厂之前不应该使用(:all)。 – surendar