Created
January 12, 2014 17:43
-
-
Save suras/8387914 to your computer and use it in GitHub Desktop.
rspec with devise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
# This spec was generated by rspec-rails when you ran the scaffold generator. | |
# It demonstrates how one might use RSpec to specify the controller code that | |
# was generated by Rails when you ran the scaffold generator. | |
# | |
# It assumes that the implementation code is generated by the rails scaffold | |
# generator. If you are using any extension libraries to generate different | |
# controller code, this generated spec may or may not pass. | |
# | |
# It only uses APIs available in rails and/or rspec-rails. There are a number | |
# of tools you can use to make these specs even more expressive, but we're | |
# sticking to rails and rspec-rails APIs to keep things simple and stable. | |
# | |
# Compared to earlier versions of this generator, there is very limited use of | |
# stubs and message expectations in this spec. Stubs are only used when there | |
# is no simpler way to get a handle on the object needed for the example. | |
# Message expectations are only used when there is no simpler way to specify | |
# that an instance is receiving a specific message. | |
describe BooksController do | |
before(:each) do | |
@user = FactoryGirl.create(:user) | |
end | |
# This should return the minimal set of attributes required to create a valid | |
# Book. As you add validations to Book, be sure to | |
# adjust the attributes here as well. | |
let(:valid_attributes) { { "title" => "bookname", "user_id" => @user.id} } | |
# This should return the minimal set of values that should be in the session | |
# in order to pass any filters (e.g. authentication) defined in | |
# BooksController. Be sure to keep this updated too. | |
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 | |
describe "GET edit" do | |
it "assigns the requested book as @book" do | |
sign_in @user | |
book = @user.books.create!(:title => "edit") | |
get :edit, {:id => book.to_param} | |
assigns(:book).should eq(book) | |
end | |
end | |
describe "POST create" do | |
describe "with valid params" do | |
it "creates a new Book" do | |
sign_in @user | |
expect { | |
post :create, {:book => {:title => "test"}} | |
}.to change(Book, :count).by(1) | |
end | |
it "assigns a newly created book as @book" do | |
sign_in @user | |
post :create, {:book => valid_attributes} | |
assigns(:book).should be_a(Book) | |
assigns(:book).should be_persisted | |
end | |
it "redirects to the created book" do | |
sign_in @user | |
post :create, {:book => valid_attributes} | |
response.should redirect_to(Book.last) | |
end | |
end | |
describe "with invalid params" do | |
it "assigns a newly created but unsaved book as @book" do | |
sign_in @user | |
# Trigger the behavior that occurs when invalid params are submitted | |
Book.any_instance.stub(:save).and_return(false) | |
post :create, {:book => { "title" => "invalid value" }} | |
assigns(:book).should be_a_new(Book) | |
end | |
it "re-renders the 'new' template" do | |
sign_in @user | |
# Trigger the behavior that occurs when invalid params are submitted | |
Book.any_instance.stub(:save).and_return(false) | |
post :create, {:book => { "title" => "invalid value" }} | |
response.should render_template("new") | |
end | |
end | |
end | |
describe "PUT update" do | |
describe "with valid params" do | |
it "updates the requested book" do | |
sign_in @user | |
book = Book.create! valid_attributes | |
# Assuming there are no other books in the database, this | |
# specifies that the Book created on the previous line | |
# receives the :update_attributes message with whatever params are | |
# submitted in the request. | |
Book.any_instance.should_receive(:update).with({ "title" => "MyString" }) | |
put :update, {:id => book.to_param, :book => { "title" => "MyString" }} | |
end | |
it "assigns the requested book as @book" do | |
sign_in @user | |
book = Book.create! valid_attributes | |
put :update, {:id => book.to_param, :book => valid_attributes} | |
assigns(:book).should eq(book) | |
end | |
it "redirects to the book" do | |
sign_in @user | |
book = Book.create! valid_attributes | |
put :update, {:id => book.to_param, :book => valid_attributes} | |
response.should redirect_to(book) | |
end | |
end | |
describe "with invalid params" do | |
it "assigns the book as @book" do | |
sign_in @user | |
book = Book.create! valid_attributes | |
# Trigger the behavior that occurs when invalid params are submitted | |
Book.any_instance.stub(:save).and_return(false) | |
put :update, {:id => book.to_param, :book => { "title" => "invalid value" }} | |
assigns(:book).should eq(book) | |
end | |
it "re-renders the 'edit' template" do | |
sign_in @user | |
book = Book.create! valid_attributes | |
# Trigger the behavior that occurs when invalid params are submitted | |
Book.any_instance.stub(:save).and_return(false) | |
put :update, {:id => book.to_param, :book => { "title" => "invalid value" }} | |
response.should render_template("edit") | |
end | |
end | |
end | |
# describe "DELETE destroy" do | |
# it "destroys the requested book" do | |
# book = Book.create! valid_attributes | |
# expect { | |
# delete :destroy, {:id => book.to_param} | |
# }.to change(Book, :count).by(-1) | |
# end | |
# it "redirects to the books list" do | |
# book = Book.create! valid_attributes | |
# delete :destroy, {:id => book.to_param} | |
# response.should redirect_to(books_url) | |
# end | |
# end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment