需要帮助来编写茉莉花规格

时间:2012-11-01 21:03:21

标签: ruby-on-rails backbone.js jasmine

您好我正在尝试为骨干视图及其功能之一编写茉莉花测试。我想在用户检查渲染视图中的复选框然后提交的情况下测试函数的正确行为。

以下是测试:

describe("buildCI()", function() {
    describe("with a category selection allowed's quidget model", function() {
        it("returns a CoacheeIssue model with the selected categories", function() {
            selection_allowed = true;
            lcc_selection_allowed = false;

            var view = new Rails3DeviseRspecCucumber.Views.CategoryPicker({
                collection: categoriesCollection,
                answers: answers,
                category_ids: category_ids,
                credentials: credentialsCollection,
                user_hash: user_hash,
                selection_allowed: selection_allowed,
                lcc_selection_allowed: lcc_selection_allowed  
            });
            // render the view so we can manipulate its DOM elements
            view.render();
            elDebug = $(view.$el);
            // Check programmatically a category checkbox
            $(elDebug.find('input.category-checkbox#5061c6a48624da6f4100000a')[0]).prop('checked', true);
            // call the buildCI() function and check the result
            result = view.buildCI();
            console.log(result);
            expect(result.get('categories')).toContain('category one');
            expect(result.get('categories')).not.toContain('category two');
        })
    })

不幸的是,测试失败并显示以下消息:预期[]包含“类别1”。 我知道这不是编码错误,因为它在现场工作,我只想知道如何测试它。

这是功能:

buildCI: () ->
# prepare the category_ids and categories (names) attributes
if @selection_allowed
  selectedCategories = []
  for checkbox in $('input.category-checkbox')
    checkboxEl = $(checkbox)
    if checkbox.checked
      selectedCategories.push(_.find(@collection.models, (model) ->
        model.id == checkboxEl.attr('id')
        ))
  category_names = _.map(selectedCategories, (category) -> category.get('name'))
  category_ids = _.map(selectedCategories, (category) -> category.get('_id'))
else
  category_names = _.map(@collection.models, (category) -> category.get('name'))
  category_ids = _.map(@collection.models, (category) -> category.get('_id'))
return new Rails3DeviseRspecCucumber.Models.CoacheeIssue({ 
      is_solved: false, status: 'active', solution_value_estimate: '',
      answers: @answers, categories: category_names, category_ids: category_ids
    })

提前致谢

1 个答案:

答案 0 :(得分:1)

你的选择器太严格了吗?我注意到它是:

$(elDebug.find('input.category-checkbox#5061c6a48624da6f4100000a')[0]).prop('checked', true);

但也许你只希望它只是:

$(elDebug.find('input.category-checkbox')[0]).prop('checked', true);