Domanda

i have used devise in rspec testing. this is my test

    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

factory

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

book controller

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

error

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

the problem is the third test "get new" fails when i run the test as a whole but passes when i run it individually. and also if i remove the before_authenticate! in controller then all test passes. Again if i commented out the "assigns" in first two describe blocks then all tests pass again. i am using rails 4.0.2 and rspec 2.14.7 , devise 3.2.2

È stato utile?

Soluzione

The only thing I can figure is that your authenticate_user method is failing for users that have previously been authenticated. It's not affecting show because you don't have :show listed in your before_action. You could test this theory by requiring authentication for show as well and seeing if your second example starts failing for before(:all) as well.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top