0

我如何编写一个测试来确保几个字段在关闭时隐藏但在打开时可见?

我目前的测试正在通过不应该的地方。正如您在下面看到的,我已经尝试使用 have_field 和 xpath 变体来测试 Capybara。(也尝试了have_css,那里也没有运气。我也在尝试测试可见性,因为测试一直在通过,所以似乎没有测试过。)

我正在做 TDD,所以需要我的测试在通过之前失败,所以我想指出 JavaScript 还没有完成。完成后,需要将“结束时间”字段中显示的时间调整为开始时间 + 2 小时 (@default_finish_time)。目前它只显示与开始时间相同的时间。

因此,“结束时间”字段是否显示 @default_finish_time 的测试应该失败,但不是。有人知道发生了什么吗?

将不胜感激,它目前正在我的脑海中。

        describe "toggling the 'Add finish time?' toggle on", :js => true do

            before {
                @default_finish_time = starts.change(:hour => 2)
                click_link "Add finish time?"               
            }

            # Tests to ensure that toggling this on, with only a start date entered, means the 
            # finish date field inherits whatever's in the start date field and the finish time
            # field inherits whatever's in the start time field + 2 hours (default event length).

            it { should have_css("#end_datetime_fields", 
                            visible: true) }

            it { should have_field("End dates", 
                            with: "#{starts.to_formatted_s(:formfield_date)}") }

            it { should have_field("End time", 
                            with: "#{starts.to_formatted_s(:formfield_time)}") }

            it { should have_field("End time", 
                            with: "#{@default_finish_time.to_formatted_s(:formfield_time)}", 
                            visible: true) }

            it { should have_xpath("//input[@value='#{starts.to_formatted_s(:formfield_time)}']") }

            it { should have_xpath("//input[@value='#{starts.change(:hour => 2).to_formatted_s(:formfield_time)}']") }

我的观点:

<div class="control-group">
    <div class="controls span12">
        <%  # If JS is enabled, end date & time fields are hidden for progressive-disclosure 
            # purposes and we need a toggle to display them upon user request.
        %>
        <%= link_to("Add finish time?", {}, id: "specify_end_time") %>
    </div>
</div>

<div class="control-group" id="end_datetime_fields">
    <div class="controls span2">
        <%= f.input :end_date_text, as: :string, 
                    :default => @event.end_date_text,
                    :input_html => { class: "date-field" },
                    :label => "End date" %>
    </div>
    <div class="controls span2">
        <%= f.input :end_time_text, as: :string, 
                    :default => @event.end_time_text,
                    :input_html => { class: "time-field" },
                    :label => "End time" %>
    </div>
    <div class="controls span2">
        <%= link_to("Remove finish time", {}, id: "remove_end_time") %>
    </div>
</div>

还有我的 JavaScript:

<script>
$(document).ready(function(){

    // Hide end date/time fields by default and display them
    // only on user's request (when an end date or time need to be recorded).
    if ( $("#event_end_date_text").val == "" ) {
        $("#end_datetime_fields").hide();
    }

    $("#specify_end_time").click(function(){
        // Load up fields using data from start date/time fields (if filled out)
        $('#event_end_date_text').val(
            $('#event_start_date_text').val()
        );
        $('#event_end_time_text').val(
            // TODO: Add two hours to it before assigning the value to the field
            $('#event_start_time_text').val()
        );

        // Show fields (ie., show the container they're in)
        $("div#end_datetime_fields").show();

        // Hide toggle
        $("#specify_end_time").hide();

        return false;
    });


    $("#remove_end_time").click(function(){
        // Empty field contents
        $("#event_end_date_text").val("");
        $("#event_end_time_text").val("");

        // Hide fields (ie., hide the container they're in)
        $("div#end_datetime_fields").hide();

        // Show toggle
        $("#specify_end_time").show();

        return false;
    });
});
</script>
4

1 回答 1

1

因为这些更改是受 Javascript 影响的,Capybara/RSpec 将无法获取它们。

如果你想测试 Javascript 逻辑,你会想要研究 Selenium 或 Jasime 之类的东西

于 2013-03-29T14:36:28.137 回答