5

首先,我有一个工作的 rails 'show' 页面,显示项目名称和属于该项目的条目。当使用 angular $scope 显示项目名称并使用 ERB 中的块显示条目时,我的测试通过了。当我用角度指令“ng-repeat”替换条目 ERB 代码时,只有我的条目测试场景开始失败。有趣的是,该应用程序仍在浏览器中运行。请记住,在我看来,另一个 $scope 变量曾经并且仍然使用几乎相同的测试通过。

工作 show.html.erb(在 ERB 中查看的条目):

<div ng-controller="ProjectCtrl">
  <h1>This is {{ project.details.name }}</h1>

  <h2>Entries</h2>
  <% @entries.each do |e| %>
  <ul>
    <li>
        <%= e.title %>
    </li>
    <li>
        <%= e.summary %>
    </li>
  </ul>
  <% end %>
</div>

破坏 show.html.erb(在 Angular 中查看的条目):

<div ng-controller="ProjectCtrl">
  <h1>This is {{ project.details.name }}</h1>

  <h2>Entries</h2>
  <ul ng-repeat=" e in project.entries ">
    <li>
        {{ e.title }}
    </li>
    <li>
        {{ e.summary }}
    </li>
  </ul>
</div>

Angular 控制器,数据已替换为返回的 JSON。

@ProjectCtrl = ["$scope", "$http", ($scope, $http) ->
  $http.get().success (data) ->
    $scope.project = {"details":{"name":"Project1","author":"brian"},"updated_at":"2013-04-13T16:48:46.406Z","entries":[{"title":"Test Title","summary":"Summary Test"},{"title":"The Third Entry","summary":"Summary of Third Entry"}]}

]

这是一个示例测试,之前有效,但在用 ng-repeat 替换 ERB 后失败:

scenario "Displays Entries Summary" do
  project = Project.create!(details: {name: "aproject"})
  Entry.create!(data: {summary: "Should Be Displayed"}, project_id: project.id)
  Entry.create!(data: {summary: "Should Not Be Displayed"})
  visit project_path(project.id)
  page.must_have_content "Should Be Displayed"
  page.wont_have_content "Should Not Be Displayed"
end

我是否遗漏了什么,或者我是否必须改变我进行功能测试的方式?

4

2 回答 2

5

为了完成这项工作,我将 Capybara 的 javascript 驱动程序设置为 poltergeist。这需要在我的集成测试中安装 phantomJS (>= 1.8.1) 并将 js 设置为 true。

这是过程:

安装 PhantomJS 1.9(显示 32 位操作系统方向的 ubuntu 12.10,相应调整):

$ cd
$ wget https://phantomjs.googlecode.com/files/phantomjs-1.9.0-linux-i686.tar.bz2
$ tar jxvf https://phantomjs.googlecode.com/files/phantomjs-1.9.0-linux-i686.tar.bz2
$ sudo mv phantomjs-1.9.0-linux-i686.tar.bz2
$ phantomjs --version

将 poltergeist 添加到 Gemfile 并捆绑安装:

group :test do
  gem 'poltergeist'
end

在“test_helper.rb”文件或类似文件中:

require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

在您的集成/功能测试中,请务必像这样包含“js: true”:

scenario "Your test with Angular.js views", js: true do
  # test is here
end

# OR

it "Your test with Angular.js views", js: true do
  # test is here
end
于 2013-04-29T22:39:49.120 回答
1

在您的控制器中,您正在设置 $scope.projects 因此在模板中变量“项目”应该可用。在您粘贴的模板代码中,您在“project.entries”(单数)上“ng-repeat”,除非您打错字或粘贴伪代码,否则这也无法在浏览器中工作。

我对 Rails 功能测试知之甚少,无法告诉您它会等待所有 JS 完成后再进行断言。Angular 中的路由变化呢?

我使用 Angular 的 JS 测试设置和 karma 和 Jasmine 来测试 Angular 页面。然后让 Jasmine 意识到 Angular 堆栈处于活动状态。

看看这篇文章:http ://www.yearofmoo.com/2013/01/full-spectrum-testing-with-angularjs-and-testacular.html

于 2013-04-25T20:27:02.190 回答