/
authenticated_articles_route_spec.rb
70 lines (57 loc) · 1.59 KB
/
authenticated_articles_route_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
module Step
def authenticated_articles_route_spec
spec_file = "#{path}/spec/requests/articles_spec.rb"
print " > editing requests spec #{spec_file}\n"
old_given = <<-RUBY
given "a article exists" do
Article.all.destroy!
request(resource(:articles), :method => "POST",
:params => { :article => { :id => nil }})
end
RUBY
gsub_file spec_file, /(^given.*?end)/mi do |match|
<<-RUBY
given "a article exists" do
Article.all.destroy!
login
request(resource(:articles), :method => "POST",
:params => { :article => {:title => 'intro', :author => 'Matt', :created_at => '2008-11-07 10:07:12' }})
end
RUBY
end
add_login_to_before_filter
add_login_spec_helpers
end
def add_login_to_before_filter
matcher = /before\(:each\) do\n*(.*?end)/mi
gsub_file "#{path}/spec/requests/articles_spec.rb", matcher do |matched_str|
<<-RUBY
before(:each) do
login
#{matched_str.match(matcher).captures.first.strip}
RUBY
end
end
def add_login_spec_helpers
test_helpers = <<-RUBY
Merb::Test.add_helpers do
def create_default_user
unless User.first(:login => "krusty")
User.create(:login => "krusty", :password => "klown", :password_confirmation => "klown") or raise "can't create user'"
end
end
def login
create_default_user
request("/login", {
:method => "PUT",
:params => {
:login => "krusty",
:password => "klown"
}
})
end
end
RUBY
append_to_file("#{path}/spec/spec_helper.rb", "#{test_helpers}")
end
end