ruby on rails-请求javascript规范失败,设计身份验证

ruby on rails - Request specs for javascript failing, devise authentication

本文关键字:失败 身份验证 rails- on 请求 javascript 范失败 ruby      更新时间:2023-09-26

我正试图在一个需要身份验证的web应用程序上测试一个操作(通过Devise)。具体操作使用javascript,因此我将js选项应用于规范,如下所示:

scenario "User wants to fax a single document", js: true do
  reset_email
  @doc = @user.documents.create(FactoryGirl.attributes_for(:document))
  visit "/documents/#{@user.id}"
  click_on "send_document_#{@doc.id}"
  last_email.should eq(@doc)
end

控制器以类似电子邮件的方式发送传真。我不知道为什么;我没有写。无论如何,在这个功能规范的顶部(使用带有Rspec的Capybara),我使用进行身份验证

before(:each) do
  # Signs in as an admin
  @company = FactoryGirl.create(:company)
  @subscription = FactoryGirl.create(:admin_subscription, company_id: @company.id)
  @user = FactoryGirl.create(:user, company_id: @company.id)
  login_as @user, scope: :user
end

文件中的所有其他规范(也需要登录)仍然通过,这让我认为这与javascript有关。因此,js选项会在Firefox中打开一个浏览器,并且页面内容不正确。上面写着

500 Internal Server Error
  undefined method `status' for nil:NilClass

我在网上搜索了一下,只找到了一些回复,说不要在我的控制器responseaction中执行操作。请放心,我没有这样的行动;只有RESTful操作以及两个附加操作:

def send_document
  @to = params[:to].gsub(/([() '-]+)/, '')
  #The below parses the number to format it for InterFax
  #It adds a "+1" to the front, and a dash in the middle 
  #of the number where needed.
  @to = "+1"+@to[0..-5]+"-"+@to[-4,4]
  @document = Document.find(params[:id])
  DocumentMailer.send_document(@to, @document).deliver
  render :template => false, :text => 'true'
end
def email_document
  @to = params[:to]
  @document = Document.find(params[:id])
  DocumentMailer.email_document(@to, @document).deliver
  render :template => false, :text => 'true'
end

有人能帮助理解这些错误吗?这个应用程序中有很多使用javascript,我真的需要一种在登录时测试这些操作的方法。

在没有首先测试操作是否已完成的情况下,请小心检查非UI条件。当你这样做:

  click_on "send_document_#{@doc.id}"
  last_email.should eq(@doc)

请记住,javascript在浏览器实例中运行,该实例与测试代码不在同一进程中。在继续之前检查页面上的更新可能会有所帮助:

  click_on "send_document_#{@doc.id}"
  page.should have_content("Email sent") # for example
  last_email.should eq(@doc)

Capybara声称在等待页面元素可见方面非常聪明——YMMV。

如果你的应用程序需要登录,你的请求规范应该使用正常的登录过程——访问登录页面,键入凭据,然后导航到页面进行测试。