1

目前,当我从我的 Rspec 测试中调用它时,以下工作:

def login_as(role)
  @role = Role.create! :name => role
  @virginia = User.create!(
    :username => "Virginia", 
    :password => "password", 
    :password_confirmation => "password", 
    :email => "example@example.com")
  @assignment = Assignment.create! user_id: @virginia.id, role_id: @role.id
  visit login_path
  fill_in "user_session_username", :with => @virginia.username
  fill_in "user_session_password", :with => @virginia.password
  click_on "submit_user_session"
end

鉴于我已经测试过界面,我认为它可能会加快速度而不是数百次点击界面,因为我的许多示例仅在用户具有角色并登录时才有意义。所以,我尝试了这个:

def login_as(role)
  @role = Role.create! :name => role
  @virginia = User.create!(
    :username => "Virginia", 
    :password => "password", 
    :password_confirmation => "password", 
    :email => "example@example.com")
  @assignment = Assignment.create! user_id: @virginia.id, role_id: @role.id
  @user_session = UserSession.create! :username => @virginia.username, :password => @virginia.password
end

但是,那个版本没有让我登录。有什么想法吗?

需要明确的是,我不是在问如何测试 declarative_authorization。我在问如何在我测试其他东西时快速登录。例如,我用login_as(role)这样的测试product#delete(只有管理员可以访问的操作):

login_as "admin"
visit product_path(@search)
click_link "delete_product"
page.current_path.should == products_path
page.should_not have_content "Retail Site Search" 
4

0 回答 0