Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8506045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:29:16+00:00 2026-06-11T02:29:16+00:00

I have the given testing code: describe ‘A new user’, js: true do before

  • 0

I have the given testing code:


describe 'A new user', js: true do
  before do
    @new_user = Fabricate.build(:user)
  end

  it 'should sign up' do
    #login code
    visit '/'
    click_link 'Login'
    fill_in 'user[email]', :with => @new_user.email
    fill_in 'user[password]', :with => @new_user.password
    click_button 'Login now'
    #login code end

    page.should have_content("Hello #{@new_user.first_name}!")
    current_path.should == dashboard_path
  end

  it 'should receive a confirmation mail' do
    #same login code again
    visit '/'
    click_link 'Login'
    fill_in 'user[email]', :with => @new_user.email
    fill_in 'user[password]', :with => @new_user.password
    click_button 'Login now'

    mail = ActionMailer::Base.deliveries.last
    assert_equal @new_user.email, mail['to'].to_s
  end
end

Now I want to add more tests.
To avoid code doubling, how can I run the capybara login code once before all tests?
One solution would be to put the login code in the before method. Another would be to create a method do_login, put the code in it and run every test like this:

it 'should do something after login' do
  do_login
  #test code here
end

But for both solutions, the code is run for every test and thats not what I want. Putting the login code in a before(:all) doesn’t work, too.

How can I run some capybara code once and then do all the tests after this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:29:18+00:00Added an answer on June 11, 2026 at 2:29 am

    You can’t run capybara code once and then run all the tests. You always start from scratch. Your proposed solution with before(:each) or helper method is the only posibility. (It’s possible to run some ruby before(:all) e.g. create objects outside the transaction check here but not Capybara)

    To speed up your specs you can test login feature in separate spec and then somehow stub the authentication but it depends on your implementation.

    If you are using Devise check devise wiki: https://github.com/plataformatec/devise/wiki/How-To:-Controllers-tests-with-Rails-3-(and-rspec)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have developed an web application[ERP FOR A SCHOOL].i have given the build for
I have got a code snippet as follows: Dim fstream = new filestream(some file
I find unit testing vital. Several times I have tried the TDD but given
i have a little weird task. The following (stripped) code is given (but i
I have created some code that emails an html email to a given address.
How do I pass arguments using ActionScript's event-listener? I have code, as given below,
I don't have much experience doing unit testing. From what I learned, code should
I have the following code as a middleware in an pylons application: import testing.model
I have a method which I am testing. Given certain inputs, it should write
I have given a default value 2.5 for a textbox called diff_Box and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.