簡體   English   中英

使用rspec測試進行設計由於prevoius測試而失敗

[英]devise with rspec test fails due to prevoius tests

我在rspec測試中使用過devise。 這是我的考驗

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

問題是當我整體運行測試時,第三個測試“ get new”失敗,但是當我單獨運行它時通過。 並且如果我刪除了before_authenticate! 在控制器中,然后所有測試通過。 同樣,如果我在前兩個描述塊中注釋掉了“賦值”,那么所有測試都會再次通過。 我正在使用Rails 4.0.2和rspec 2.14.7,設計3.2.2

我唯一能想到的是,對於以前已通過身份驗證的用戶,您的authenticate_user方法失敗。 這不會影響show因為您的before_action沒有列出:show 您可以通過要求對show進行身份驗證並查看您的第二個示例是否也針對before(:all)開始失敗來測試該理論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM