我有一个非常基本的,集成的rails-angular应用程序。它只是呈现一个链接,它使用来自获取请求的数据到rails:
'use strict';
angular.module('angularRspec.controllers')
.controller('FightersController',
function($scope, $http){
console.log("FightersController is loaded...");
$scope.fighters = [];
$http.get('./fighters.json').
success(function(data, status){
$scope.fighters = data;
console.log(status);
console.log(data);
}).
error(function(data, status, headers, config) {
console.log(status);
console.log(headers);
});
});
当我运行这样的规范并使用pry时,我的链接不会出现:
require 'rails_helper'
feature 'Index of Fighters' do
let!(:fighter) { create(:fighter) }
scenario 'User visits home', js: true do
visit '/'
binding.pry
expect(true).to eq true
end
end
但是,我已从控制台确认测试浏览器中正在加载角度,响应为200.该问题似乎不是浏览器特定的。我在这里发布了一个回购:angular_rspec。
这是我的spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
Capybara.javascript_driver = :selenium
end
这是我的rails_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.check_pending!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
这是我的布局:
<!DOCTYPE html>
<html lang="en" ng-app="angularRspec">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "angular rspec" %></title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag "application", :media => "all" %>
</head>
<body>
<div class="container">
<h1>Angular Rspec</h1>
<div ng-view></div>
<div ng-controller="FightersController" id="primary"></div>
</div>
<%= javascript_include_tag "application" %>
</body>
</html>
答案 0 :(得分:2)
我的猜测是你的create(:fighter)
调用在与AJAX get请求不同的线程(和数据库事务)中运行。请尝试将此代码段添加到rails_helper
。
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
来源:https://github.com/jnicklas/capybara#transactions-and-database-setup
答案 1 :(得分:0)
您需要为capybara启用javascript驱动程序:
Capybara.current_driver = Capybara.javascript_driver