-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbookmarks_controller_spec.rb
180 lines (148 loc) · 6.04 KB
/
bookmarks_controller_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# encoding: utf-8
# Copyright (C) 2012 TEA, the ebook alternative <http://www.tea-ebook.com/>
#
# This file is part of TeaBook Open Reader
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.0 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# An additional permission has been granted as a special exception
# to the GNU General Public Licence.
# You should have received a copy of this exception. If not, see
# <https://github.com/TEA-ebook/teabook-open-reader/blob/master/GPL-3-EXCEPTION>.
require 'spec_helper'
describe BookmarksController do
let(:book) { Fabricate(:ebook_epub) }
let(:bookmark) { Fabricate(:bookmark, book: book) }
let(:user) { Fabricate(:user, books: [book]) }
before :each do
sign_in user
end
it "should have a current_user" do
subject.current_user.should_not be_nil
end
# This should return the minimal set of attributes required to create a valid
# Bookmark. As you add validations to Bookmark, be sure to
# update the return value of this method accordingly.
def valid_attributes
{book_id: book.to_param, component_name: book.components.first.src}
end
def create_a_bookmark!
book.update_attributes!(bookmarks: [bookmark])
end
describe "GET index" do
before { create_a_bookmark! }
it "assigns all bookmarks as @bookmarks" do
get :index, {book_id: book.to_param}
assigns(:bookmarks).should eq([bookmark])
end
end
describe "GET show" do
before { create_a_bookmark! }
it "assigns the requested bookmark as @bookmark" do
get :show, {:id => bookmark.to_param, book_id: book.to_param}
assigns(:bookmark).should eq(bookmark)
end
end
describe "GET new" do
it "assigns a new bookmark as @bookmark" do
get :new, {book_id: book.to_param}
assigns(:bookmark).should be_a_new(Bookmark)
end
end
describe "GET edit" do
before { create_a_bookmark! }
it "assigns the requested bookmark as @bookmark" do
get :edit, {:id => bookmark.to_param, book_id: book.to_param}
assigns(:bookmark).should eq(bookmark)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new Bookmark" do
expect {
post :create, {book_id: book.to_param, bookmark: valid_attributes}
}.to change(Bookmark, :count).by(1)
end
it "assigns a newly created bookmark as @bookmark" do
post :create, {book_id: book.to_param, bookmark: valid_attributes}
assigns(:bookmark).should be_a(Bookmark)
assigns(:bookmark).should be_persisted
end
it "redirects to the created bookmark" do
post :create, {book_id: book.to_param, bookmark: valid_attributes}
response.should redirect_to(book_bookmark_url(book, Bookmark.last))
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved bookmark as @bookmark" do
# Trigger the behavior that occurs when invalid params are submitted
Bookmark.any_instance.stub(:save).and_return(false)
post :create, {book_id: book.to_param, bookmark: {}}
assigns(:bookmark).should be_a_new(Bookmark)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
Bookmark.any_instance.stub(:save).and_return(false)
post :create, {book_id: book.to_param, bookmark: {}}
response.should render_template("new")
end
end
end
describe "PUT update" do
before { create_a_bookmark! }
describe "with valid params" do
it "updates the requested bookmark" do
# Assuming there are no other bookmarks in the database, this
# specifies that the Bookmark created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
Bookmark.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
put :update, {:id => bookmark.to_param, book_id: book.to_param, bookmark: {'these' => 'params'}}
end
it "assigns the requested bookmark as @bookmark" do
put :update, {:id => bookmark.to_param, book_id: book.to_param, bookmark: valid_attributes}
assigns(:bookmark).should eq(bookmark)
end
it "redirects to the bookmark" do
put :update, {:id => bookmark.to_param, book_id: book.to_param, bookmark: valid_attributes}
response.should redirect_to(book_bookmark_path(book))
end
end
describe "with invalid params" do
it "assigns the bookmark as @bookmark" do
# Trigger the behavior that occurs when invalid params are submitted
Bookmark.any_instance.stub(:save).and_return(false)
put :update, {:id => bookmark.to_param, book_id: book.to_param, bookmark: {}}
assigns(:bookmark).should eq(bookmark)
end
it "re-renders the 'edit' template" do
# Trigger the behavior that occurs when invalid params are submitted
Bookmark.any_instance.stub(:save).and_return(false)
put :update, {:id => bookmark.to_param, book_id: book.to_param, bookmark: {}}
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
before { create_a_bookmark! }
it "destroys the requested bookmark" do
expect {
delete :destroy, {:id => bookmark.to_param, book_id: book.to_param}
}.to change(Bookmark, :count).by(-1)
end
it "redirects to the bookmarks list" do
delete :destroy, {:id => bookmark.to_param, book_id: book.to_param}
response.should redirect_to(book_bookmarks_path(book))
end
end
end