[英]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.