How do I re-use Capybara sessions between tests?

后端 未结 4 1126
别那么骄傲
别那么骄傲 2021-02-05 09:27

I want to keep on using the same session and by that I mean Rails\' session between various Test::Unit integration tests that use Capybara. The Capybara::Sess

4条回答
  •  名媛妹妹
    2021-02-05 09:48

    If what you are doing is trying to string together individual examples into a story (cucumber style, but without cucumber), you can use a gem called rspec-steps to accomplish this. For example, normally this won't work:

    describe "logging in" do
      it "when I visit the sign-in page" do 
        visit "/login"
      end
      it "and I fill in my registration info and click submit" do
        fill_in :username, :with => 'Foo'
        fill_in :password, :with => 'foobar'
        click_on "Submit"
      end
      it "should show a successful login" do
        page.should have_content("Successfully logged in")
      end
    end
    

    Because rspec rolls back all of its instance variables, sessions, cookies, etc.

    If you install rspec-steps (note: currently not compatible with rspec newer than 2.9), you can replace 'describe' with 'steps' and Rspec and capybara will preserve state between the examples, allowing you to build a longer story, e.g.:

    steps "logging in" do
      it "when I visit the sign-in page" #... etc.
      it "and I fill in" # ... etc.
      it "should show a successful" # ... etc.
    end
    

提交回复
热议问题