如何调整测试助手以考虑测试用户的模型(元编程)

时间:2015-04-20 17:47:49

标签: ruby-on-rails ruby ruby-on-rails-4

我跟着Hartl's chapter 8开发了一个log_in_as测试帮助器。现在我想通过添加类型作为参数来进行一些元编程。 Type指定用户来自哪个模型。有两种模型(成员和组织),每种模型都可以使用相同的页面/创建方法登录(通过if-else语句)。

但是,对于集成测试,测试无效。测试始终将用户视为来自模型组织,而不是来自成员。我做错了什么?

我有以下测试助手

  def log_in_as(type, user, options = {})
    password    = options[:password]    || 'password'
    remember_me = options[:remember_me] || '1'
    ### Additional if-else statement needed for type? ###
    if integration_test?
      post login_path, session: { email:       user.email,
                                  password:    password,
                                  remember_me: remember_me }
    else
      if type == "mem_ber"
        session[:member_id] = user.id
      elsif type == "org_anization"
        session[:organization_id] = user.id
      end
    end
  end

  def is_logged_in_member?
    !session[:member_id].nil?
  end

  def is_logged_in_organization?
    !session[:organization_id].nil?
  end

  private
    # Returns true inside an integration test.
    def integration_test?
      defined?(post_via_redirect)
    end

集成测试

require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
  def setup
    @admin         = members(:michael)
    @non_admin     = members(:archer)
    @org_admin     = organizations(:one)
    @org_nonadmin  = organizations(:two)
  end

  test "ff" do
    log_in_as("mem_ber", @admin)
    puts "member: #{is_logged_in_member?}"
    puts "organization: #{is_logged_in_organization?}"
    delete logout_path

    log_in_as("mem_ber", @non_admin)
    puts "member: #{is_logged_in_member?}"
    puts "organization: #{is_logged_in_organization?}"
    delete logout_path

    log_in_as("org_anization", @org_admin)
    puts "member: #{is_logged_in_member?}"
    puts "organization: #{is_logged_in_organization?}"
    delete logout_path

    log_in_as("org_anization", @org_nonadmin)
    puts "member: #{is_logged_in_member?}"
    puts "organization: #{is_logged_in_organization?}"
    delete logout_path
  end
end

会话控制器中的Def create是:

  def create
    @organization = Organization.find_by(email: params[:session][:email].downcase)
    @member = Member.find_by(email: params[:session][:email].downcase)
    if @organization && @organization.authenticate(params[:session][:password])
      if @organization.activated?
        log_in("org_anization", @organization)
        params[:session][:remember_me] == '1' ? remember("org_anization", @organization) : forget("org_anization", @organization)
        redirect_back_or @organization
      else
        message  = "Account not activated. "
        message += "Check your email for the activation link."
        flash[:warning] = message
        redirect_to root_url
      end
    elsif @member && @member.authenticate(params[:session][:password])
      if @member.activated?
        log_in("mem_ber", @member)
        params[:session][:remember_me] == '1' ? remember("mem_ber", @member) : forget("mem_ber", @member)
        redirect_back_or @member
      else
        message  = "Account not activated. "
        message += "Check your email for the activation link."
        flash[:warning] = message
        redirect_to root_url
      end
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

对于测试文件中的所有四个log_in_as测试,它表示is_logged_in_member? false和is_logged_in_organization?为true,所以如果它实际上是来自members.yml的夹具。我也尝试过添加

  self.set_fixture_class organizations: Organization
  self.set_fixture_class members: Member

给测试助手,但没有区别。

如果我从测试中移除组织(在def setup中并删除四个测试中的两个),它仍会产生相同的结果,因为它将成员用户视为组织。只有当我也清空组织夹具文件时,它才会将成员视为成员。

2 个答案:

答案 0 :(得分:0)

test_helper.rb

self.set_fixture_class usertype1: Usertype1
self.set_fixture_class usertype2: Usertype2

答案 1 :(得分:0)

我找到了原因:事实证明,在两个灯具文件中都有一个具有相同电子邮件地址的实例。