黄瓜测试不起作用

时间:2013-05-31 19:57:17

标签: jquery ruby-on-rails cucumber

我是测试的新手,我很难弄清楚我的测试无法正常工作的原因。这是我的名为child_user_selected_categories.feature

的功能文件
@no-database-cleaner
Feature: Managing selected_categories
  In order to use portal
  I want to create selected_categories for child user


  Scenario:  Creating a child selected_categories for a child user
    Given the database contains no test data
    And an user and sub user exists
    Given I am on the homepage

    When I attempt to sign in with following user account:
      | email address         | password |
      | abc@company1.com   | password |

    Then I should see "abc@company1.com" message on page
    When I follow "All Child Users"
    And I view "Child Categories" for sub user "xyztest@gmail.com"
    Then I should see "Default Filter Level for User xyztest@gmail.com is"
    When I press "APPLY"
    Then I should see "Custom Filter Created for xyztest@gmail.com"

我正在使用factory_girl来记录。这是我的步骤定义文件child_user_selected_categories.rb

And /^I view "([^"]*)" for sub user "([^"]*)"$/ do |link, user|
  selector = find('tr', text: user)
  within selector do
    click_link(link)
  end
end

Given(/^an user and sub user exists$/) do
  parent = FactoryGirl.create(:user)
  user = FactoryGirl.create(:user, email: "xyztest@gmail.com", parent_id: parent.id, default_filter_level: parent.default_filter_level)
  account = FactoryGirl.create(:account, user_id: parent.id)
  FactoryGirl.create(:filter, user_id: user.id, account_id: account.id)
  FactoryGirl.create(:filter, user_id: parent.id, account_id: account.id)
end

在features / support下名为user.rb的文件具有覆盖某些用户字段的代码 要求'factory_girl_rails'

FactoryGirl.define do
  factory :user do |u|
    u.email 'recdns@company1.com'
    u.password 'password'
    u.src_ip_addr 'hostname -I'
    u.ar_id true
    u.default_filter_level 'Medium'
    u.src_netmask '255.255.255.0'
  end
end

使用jquery post

申请提交按钮
//on sibmit this will create a records for user for selected categories
        $("#selectes_categories").submit(function(){

            // get alll the selected category ids in list box 2
            var options = $("select#lstBox2").children().map(function() {return $(this).val();}).get();

            //var values = $('#categories_ids_').val()
            $('#selected_category_ids').val(options);
            $.post($(this).attr("action"),$(this).serialize(),function(response){
                $("#flash_notice").html(response);
            });
            return false;
        });

<%= form_tag "/levels/create_user_selected_categories", :id=> "selectes_categories" do %>
    <%= hidden_field_tag 'selected_category_ids' %>
    <%= hidden_field_tag 'user_id',@user.id %>
    <%= submit_tag "APPLY",:class => 'btn btn-primary'%>
<% end %>

提交我的代码完成它的工作。但是当我说按APPLY时它不能使用黄瓜。我已经为几乎相同的场景编写了测试并且它们工作得非常好,但只有差异在这里我使用jquery post去控制器并创建记录。这是控制器代码

def create_user_selected_categories
    @user = User.find(params[:user_id]) if params[:user_id] != nil
    if @user != nil
      UserSelectedCategory.where(:user_id => @user.id).destroy_all
      @selected_user_categories = params[:selected_category_ids]
      @selected_user_categories.split(",").each{|catg_id|
        UserSelectedCategory.create(:user_id => @user.id, :category_id => catg_id.to_i)
      }
      @total_category_masks_for_user = Category.sum(:category_mask, :conditions => {:id => @user.user_selected_categories.map{|catg| catg.category_id}})

      @user_filter = Filter.find_by_user_id(@user.id)
      if @user_filter == nil
        Filter.create(:category_mask => @total_category_masks_for_user, :account_id => @user.account.id, :user_id => @user.id)
      else
        @user_filter.category_mask = @total_category_masks_for_user
        @user_filter.save
      end

    end
    render :action => 'create_user_selected_categories', :layout => false
  end

有什么建议吗?感谢

1 个答案:

答案 0 :(得分:1)

假设您使用Capybara和Cucumber,默认情况下不会执行您的Javascript。一个非常简单的解决方案可能是将@javascript标记添加到您的方案中:

@javascript 
Scenario:  Creating a child selected_categories for a child user
...

这将使用Selenium驱动程序运行您的方案,该驱动程序将实际打开浏览器窗口并以真实用户身份测试您的应用程序,其中包括Javascript执行。默认情况下会尝试使用Firefox,因此请确保已安装Firefox。

更多:https://github.com/jnicklas/capybara#using-capybara-with-cucumber