Module: SpecDataHelper
- Defined in:
- spec/spec_data_helper.rb
Overview
SpecDataHelper
Configured in ‘spec_helper.rb’ - config.include SpecDataHelper Functions we use in all specs to create test-data
Instance Method Summary (collapse)
-
- (Boolean) attributes_changed?(item, saved_attributes)
Boolean - true if any attribute is not equal.
-
- (Object) cleanup_database
Drop all documents of collections we’ll test.
-
- (Object) create_default_page_template
Create a default PageTemplate.
-
- (Object) create_default_userset
Create the default user set.
-
- (Boolean) create_file_if_not_exists(path, content = "")
True if file was created before otherwise false.
-
- (Object) create_news_blog_with_postings {|news_blog| ... }
Create the first “News-Blog and yield the given block with the new item” if no block given return the new item.
-
- (Page) create_page_with_component(options)
Create a page with one component.
-
- (Posting) create_posting_for(email, attributes = {})
Create a valid and visible posting for user with email If blog ‘News’ doesn’t exist, it will be created.
-
- (Object) log_in_as(user_email, password)
Login.
-
- (Object) log_out
Log out.
-
- (Object) with_user(user_email) {|user| ... }
Do something (the block) with the user-object.
Instance Method Details
- (Boolean) attributes_changed?(item, saved_attributes)
Boolean - true if any attribute is not equal.
178 179 180 181 182 183 184 185 |
# File 'spec/spec_data_helper.rb', line 178 def attributes_changed?(item,saved_attributes) item.reload saved_attributes.delete('updated_at') saved_attributes.each do |key, value| return true unless item.attributes[key].eql?(value) end false end |
- (Object) cleanup_database
Drop all documents of collections we’ll test
9 10 11 12 13 14 15 16 17 18 19 |
# File 'spec/spec_data_helper.rb', line 9 def cleanup_database begin Posting.unscoped.delete_all Blog.unscoped.delete_all User.unscoped.delete_all PageTemplate.delete_all Page.delete_all rescue => e puts "*** ERROR CLEANING UP DATABASE -- #{e.inspect}" end end |
- (Object) create_default_page_template
Create a default PageTemplate
129 130 131 |
# File 'spec/spec_data_helper.rb', line 129 def create_default_page_template PageTemplate.create(:name => 'default') end |
- (Object) create_default_userset
Create the default user set. Admin is created first because the first user will have admin-role instantly. Then create users with other roles. To use the default userset
log_in_as "_role_@iboard.cc", "thisisnotsecret"
Where ‘role’ can be one of:
admin@iboard.cc
user@iboard.cc
author@iboard.cc
moderator@iboard.cc
maintainer@iboard.cc
staff@iboard.cc
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 |
# File 'spec/spec_data_helper.rb', line 33 def create_default_userset User.unscoped.delete_all [ #ROLES = [:guest, :confirmed_user, :author, :moderator, :maintainer, :admin] #see user.rb model # # ATTENTION cba makes the first user an admin! # -> The first user of the following hash must be the admin! { :email => 'admin@iboard.cc', :name => 'admin', :roles_mask => 5, :password => 'thisisnotsecret', :password_confirmation => 'thisisnotsecret' }, # Define NON-ADMINS BELOW { :email => 'user@iboard.cc', :name => 'testmax', :roles_mask => 1, :password => 'thisisnotsecret', :password_confirmation => 'thisisnotsecret' }, { :email => 'author@iboard.cc', :name => 'Author', :roles_mask => 2, :password => 'thisisnotsecret', :password_confirmation => 'thisisnotsecret' }, { :email => 'moderator@iboard.cc', :name => 'Moderator', :roles_mask => 3, :password => 'thisisnotsecret', :password_confirmation => 'thisisnotsecret' }, { :email => 'maintainer@iboard.cc', :name => 'maintainer', :roles_mask => 4, :password => 'thisisnotsecret', :password_confirmation => 'thisisnotsecret' }, { :email => 'staff@iboard.cc', :name => 'staff', :roles_mask => 4, :password => 'thisisnotsecret', :password_confirmation => 'thisisnotsecret' } ].each do |hash| user = User.create(hash) user.confirm! user.save! user.reload raise "NOT CONFIRMED!" unless user.confirmed? end end |
- (Boolean) create_file_if_not_exists(path, content = "")
True if file was created before otherwise false
164 165 166 167 168 169 170 171 172 173 |
# File 'spec/spec_data_helper.rb', line 164 def create_file_if_not_exists(path, content="") file_exists = File.exists?(path) unless file_exists f = File.open(path,"w") f << content f.close file_exists = true end file_exists end |
- (Object) create_news_blog_with_postings {|news_blog| ... }
Create the first “News-Blog and yield the given block with the new item” if no block given return the new item
153 154 155 156 157 158 159 |
# File 'spec/spec_data_helper.rb', line 153 def create_news_blog_with_postings(&block) news_blog = Blog.find_or_create_by( title: 'News' ) unless block_given? return news_blog end yield(news_blog) end |
- (Page) create_page_with_component(options)
Create a page with one component
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'spec/spec_data_helper.rb', line 136 def create_page_with_component( ) component = [:page_component] template = create_default_page_template component.merge! :page_template_id => template.id .delete(:page_component) .merge! :page_template_id => template.id page = Page.new() unless page.valid? puts "***** PAGE INVALID IN #{__FILE__}:#{__LINE__} - #{page.errors.inspect} *****" end page.page_components.create(component) page.save! page end |
- (Posting) create_posting_for(email, attributes = {})
Create a valid and visible posting for user with email If blog ‘News’ doesn’t exist, it will be created.
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'spec/spec_data_helper.rb', line 91 def create_posting_for(email,attributes={}) user = User.where( :email => email).first attributes.merge! :user_id => user.id blog = Blog.first || Blog.create!(title: 'News', is_draft: false) posting = blog.postings.create(attributes) unless posting.valid? assert false, "# New Posting has errors #{posting.errors.inspect}\n" end posting.save! blog.save! posting end |
- (Object) log_in_as(user_email, password)
Login
114 115 116 117 118 119 120 121 |
# File 'spec/spec_data_helper.rb', line 114 def log_in_as(user_email,password) with_user(user_email) do |user| visit "/users/sign_in" fill_in("Email", :with => user.email) fill_in("Password", :with => password) ("Sign in") end end |
- (Object) log_out
Log out
124 125 126 |
# File 'spec/spec_data_helper.rb', line 124 def log_out click_link("Sign out") end |
- (Object) with_user(user_email) {|user| ... }
Do something (the block) with the user-object.
106 107 108 109 |
# File 'spec/spec_data_helper.rb', line 106 def with_user user_email, &block user = User.where(:email => user_email).first yield(user) end |