简体   繁体   English

使用minitest / capybara设置一次登录以运行Rails测试

[英]Setting up one time login with minitest/capybara for running rails tests

I'm using capybara with minitest on Rails 2.3.14 . 我在Rails minitest上使用minitest capybara Like most applications, this one also requires login to do anything inside the site. 与大多数应用程序一样,此应用程序也需要登录才能在站点内进行任何操作。 I'd like to be able to login once per test-suite and use that session throughout all tests that are run. 我希望每个测试套件可以登录一次,并在所有运行的测试中使用该会话。 How do I refactor that to the minitest_helper ? 如何将其重构为minitest_helper Right now my helper looks something like this: 现在我的助手看起来像这样:

#!/usr/bin/env ruby

ENV['RAILS_ENV'] = 'test'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")

gem 'minitest'
gem 'capybara_minitest_spec'

require 'minitest/unit'
require 'minitest/spec'
require 'minitest/mock'
require 'minitest/autorun'
require 'capybara/rails'
require 'factory_girl'


FactoryGirl.find_definitions

class MiniTest::Spec

  include FactoryGirl::Syntax::Methods
  include Capybara::DSL
  include ActionController::URLWriter

  before(:each) do
    # .. misc global setup stuff, db cleanup, etc.
  end

  after(:each) do
    # .. more misc stuff
  end

end

thanks. 谢谢。

Here's an example of multiple sessions and custom DSL in an integration test 这是集成测试中多个会话和自定义DSL的示例

require 'test_helper'

class UserFlowsTest < ActionDispatch::IntegrationTest
  fixtures :users

  test "login and browse site" do

    # User avs logs in
    avs = login(:avs)
    # User guest logs in
    guest = login(:guest)

    # Both are now available in different sessions
    assert_equal 'Welcome avs!', avs.flash[:notice]
    assert_equal 'Welcome guest!', guest.flash[:notice]

    # User avs can browse site
    avs.browses_site
    # User guest can browse site as well
    guest.browses_site

    # Continue with other assertions
  end

  private

  module CustomDsl
    def browses_site
      get "/products/all"
      assert_response :success
      assert assigns(:products)
    end
  end

  def login(user)
    open_session do |sess|
      sess.extend(CustomDsl)
      u = users(user)
      sess.https!
      sess.post "/login", :username => u.username, :password => u.password
      assert_equal '/welcome', path
      sess.https!(false)
    end
  end
end

Source : http://guides.rubyonrails.org/testing.html#helpers-available-for-integration-tests 来源: http : //guides.rubyonrails.org/testing.html#helpers-available-for-integration-tests

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 耙子测试没有在最小的时候进行水豚测试 - Rake test not picking up capybara tests in minitest 使用Minitest的Rails,如何设置RuboCop在每次使用rake运行测试时自动运行? - With Rails using Minitest, how can I set up RuboCop to run automatically each time tests are run with rake? 使用minitest水豚破坏铁轨中的测试 - destroy test in rails with minitest capybara 为Rails 2.3和Rspec设置Capybara - Setting up Capybara for Rails 2.3 and Rspec 使用Devise / Rails 4 / Minitest运行控制器测试时无法登录 - Can't sign in when running controller tests with Devise / Rails 4 / Minitest 运行Rails 4和MiniTest中生成的功能测试时,发生InvalidAuthenticityToken错误 - InvalidAuthenticityToken error while running functional tests generated in Rails 4 and MiniTest 如何在Rails中进行DRY测试并仍然使用Capybara和Warden? (使用最小测试) - How can I make tests DRY in Rails and still use Capybara and Warden? (using minitest) 使用minitest水豚栏杆测试显示/隐藏 - testing show/hide with minitest capybara rails Rails - Minitest + Capybara + Selenium - 测试破坏动作 - Rails - Minitest + Capybara + Selenium - Test destroy action 未找到使用minitest水豚路轨链接的测试 - testing with minitest capybara rails link not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM