Last active
January 13, 2022 12:20
-
-
Save BideoWego/973df3cf566b99513da8 to your computer and use it in GitHub Desktop.
Rails Spec setup checklist, helper and support files
This file contains hidden or 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
| # spec/support/controller_helper.rb | |
| module ControllerHelper | |
| def login(user) | |
| session[:user_id] = user.id | |
| end | |
| def logout | |
| session.delete(:user_id) | |
| end | |
| def current_user | |
| User.find(session[:user_id]) if User.exists?(session[:user_id]) && session[:user_id] | |
| end | |
| def signed_in_user? | |
| !!current_user | |
| end | |
| end |
This file contains hidden or 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
| # spec/support/database_cleaner.rb | |
| # From blog post | |
| # http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/ | |
| RSpec.configure do |config| | |
| config.before(:suite) do | |
| DatabaseCleaner.clean_with(:truncation) | |
| end | |
| config.before(:each) do | |
| DatabaseCleaner.strategy = :transaction | |
| end | |
| config.before(:each, :js => true) do | |
| DatabaseCleaner.strategy = :truncation | |
| end | |
| config.before(:each) do | |
| DatabaseCleaner.start | |
| end | |
| config.after(:each) do | |
| DatabaseCleaner.clean | |
| end | |
| end |
This file contains hidden or 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
| # spec/support/macros/global.rb | |
| module Macros | |
| module Global | |
| def submit_form | |
| find('input[name="commit"]').click | |
| end | |
| end | |
| end |
This file contains hidden or 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
| # spec/views/index_spec.rb | |
| require 'rails_helper' | |
| describe 'secrets/index.html.erb' do | |
| let(:user){create(:user)} | |
| let(:users){[user, create(:user)]} | |
| let(:secrets){[create(:secret, :author => user), create(:secret)]} | |
| before do | |
| view.extend(ViewHelper) | |
| assign(:users, users) | |
| assign(:secrets, secrets) | |
| end | |
| context 'the user is logged in' do | |
| it 'shows the author of all secrets' do | |
| login(user) | |
| render | |
| expect(rendered).to_not have_content(hidden_text) | |
| end | |
| end | |
| context 'the user is logged out' do | |
| it 'does not show any author of any secrets' do | |
| render | |
| expect(rendered).to have_content(hidden_text) | |
| end | |
| end | |
| end |
This file contains hidden or 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
| # ------------------------------------ | |
| # My Requires | |
| # ------------------------------------ | |
| require 'capybara/rails' | |
| Dir[ | |
| Rails.root.join("spec/support/**/*.rb") | |
| ].each { |f| require f } | |
| RSpec.configure do |config| | |
| # ------------------------------------ | |
| # My Config | |
| # ------------------------------------ | |
| config.include FactoryGirl::Syntax::Methods | |
| # config.fixture_path = "#{::Rails.root}/spec/fixtures" #<<<< comment out | |
| config.use_transactional_fixtures = false #<<<<< should be false when using FactoryGirl | |
| end |
This file contains hidden or 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
| # spec/support/view_helper.rb | |
| module ViewHelper | |
| include ControllerHelper | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment