在Capybara测试期间调用Coffeescript

问题描述 投票:0回答:2

我的应用程序有很多不同的地方,我通过在用户单击按钮/ link / div时调用ajax函数来更新数据库。我需要在我的测试中包含这些功能,但我对Capybara还不够了解。

下面是我要测试的一个div的示例,关联的ajax调用以及我想要传递的测试。我应该补充一点,我正在构建Cloud9 IDE,以防万一。

视图

<% @seminar.students.order(:last_name).in_groups_of(6).each do |group| %>
    <div class="row">
        <% group.each do |student| %>
            <% if student %>
               <% this_ss = @seminar.seminar_students.find_by(:user => student) %>
                  <% if this_ss.present %>
                       <% thisClass = "col-md-2 seat clickySeat" %>
                       <% thisText = "Present" %>
                  <% else %>
                       <% thisClass = "col-md-2 seat clickySeat absent" %>
                       <% thisText = "Absent" %>
                  <% end %>
                  <div class = "<%= thisClass %>" id="<%= this_ss.id %>" name="ss_<%= this_ss.id %>">
                      <%= student.first_plus_init %><br/>
                      <p class="presentTag"><%= thisText %></p>
                  </div>
               <% end %>
         <% end %>
    </div>
<% end %>

CoffeeScript的

if $('.clickySeat').length > 0
    $('.clickySeat').on "click", ->
        this_tag = $(this).find(".presentTag")
        seminar_student_id = $(this).attr('id')
        url = '/seminar_students/'+seminar_student_id
        if this_tag.text() == "Absent"
            attendance = true
            $(this).removeClass("absent")
            this_tab.text("Present")
        else
            attendance = false
            $(this).addClass("absent")
            this_tab.text("Absent")
        $.ajax
            type: "PUT",
            url: url,
            dataType: "json"
            data:
                seminar_student:
                    present: attendance

测试

test "attendance with click" do
    @ss = @student_3.seminar_students.find_by(:seminar => @seminar)
    assert @ss.present

    capybara_login(@teacher_1)
    click_on("desk_consult_#{@seminar.id}")
    click_on("#{new_consultancy_button_text}")
    find("##{@ss.id}").click

    assert_not @ss.present
    click_on("Create Desk Consultants Groups")
end

在开发和制作中,div和ajax正在努力切换被标记为缺席/存在于数据库中的学生。但在测试中,assert_not @ss.present行失败了。这是我试图在任何地方修复的模式:我的所有水豚测试都没有调用任何javascript。

我已经阅读了一些类似的问题和相关文章。我已经尝试了到目前为止我已阅读的所有建议,例如添加selenium-webkit gem以便我可以使用Capybara.javascript-driver =:selenium这一行。到目前为止,我遇到的解决方案都没有对我有用。

提前感谢您的任何见解。

ruby-on-rails ajax coffeescript capybara
2个回答
1
投票

运行JS的测试通常都不是两个问题中的一个,然后测试还有两个问题可能会导致失败。

对于JS问题,第一个原因可能是您的某个JS资产中存在错误,导致它们在连接在一起时不会被处理。既然你声明它在生产模式下工作(连接也会发生),我们应该能够排除这种情况。第二个原因是你没有使用支持JS的驱动程序。确保您已按照自述文件中的说明进行操作 - https://github.com/teamcapybara/capybara#using-capybara-with-minitest - 从ActionDispatch :: IntegrationTest派生您的测试类,并且在任何需要JS的测试之前,该部分的最后部分(其中Capybara.current_driver = Capybara.javascript_driver)正在运行。 (如果您希望所有测试都使用JS支持,则可以在设置Capybara.javascript_driver后设置Capybara.default_driver = Capybara.javascript_driver)。如果使用Rails <5.1,您可能还需要安装和配置database_cleaner - 请参阅https://github.com/teamcapybara/capybara#transactions-and-database-setup

这应该让您的测试运行具有JS功能的驱动程序 - 除此之外 - 首先:当您调用find(...).click时,无法保证等待点击完成导致的任何操作。这意味着您需要设置一个断言(Capybara提供的断言之一,因为它们具有等待/重试行为),以便在尝试从数据库访问元素之前通过单击触发的可见更改来确保它已完成。 第二:既然你已经加载了@ss,那么在重新加载对象之前,它不会反映数据库状态的变化。

这意味着你的测试需要改变

test "attendance with click" do
  @ss = @student_3.seminar_students.find_by(:seminar => @seminar)
  assert @ss.present

  capybara_login(@teacher_1)
  click_on("desk_consult_#{@seminar.id}")
  click_on("#{new_consultancy_button_text}")
  find("##{@ss.id}").click
  assert_text("You are marked as absent") # An assertion for whatever visible change occurs due to the previous click which will delay the next statement until the app has completed processing
  assert_not @ss.reload.present # Need to reload the object to see any DB changes
  click_on("Create Desk Consultants Groups") 
end

需要一直重新加载对象是因为对DB对象进行断言通常不赞成的原因之一,你应该主要只测试UI行为/更改(在功能测试中)


1
投票

您可以尝试使用Poltergeist驱动程序并更改配置以使用它。不要忘记在你的路径上安装PhantomJS(你可以使用命令:npm install -g phantomjs安装它)。

你应该在测试的某些部分添加js: true,我只是不记得在哪里。但是,你可以搜索它。 =]

祝好运。

© www.soinside.com 2019 - 2024. All rights reserved.