![](/img/trans.png)
[英]Rspec test fails due to to redirection to a page for loggin in (Rails + Devise + Cancancan)
[英]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.