我确实有一个使用 Capybara / Rspec 的 Rails 应用程序(Rails 4 / Mongoid)。此外,我确实将 Angular.js 用于一些客户端的东西。不幸的是,我在规格测试中遇到了奇怪的行为。
RuntimeError: Circular dependency detected while autoloading constant Offer
creating_offers_spec.rb
require 'spec_helper'
feature 'creating offers' do
before do
visit '/offers/new'
end
scenario 'can create an offer', js: true do
offer_number = Random.rand(10000)
fill_in 'offer_number', with: offer_number
fill_in 'offer_description', with: 'hello world!'
click_button 'btn_submit'
offer_in_db = Offer.where(number: offer_number).first
expect(offer_in_db.description).to eql 'hello world!'
end
end
作为 Capybara 的 JavaScript 驱动程序,我确实使用 poltergeist。
OfferController.js.coffee
angular.module('exampleApp').controller 'OffersController', ($scope, Offer) ->
$scope.offer = {}
$scope.offer.positions = []
$scope.addOffer = ->
new Offer().create($scope.offer)
$scope.addPosition = ->
position = { order: $scope.offer.positions.length + 1 }
$scope.offer.positions.push(position)
OfferService.js.coffee
angular.module('exampleApp').factory 'Offer', ($resource, $http) ->
class Offer
constructor: ->
@service = $resource('/api/offers/:offersId', {offersId: '@id'}, {update: {method: 'PATCH'}})
defaults = $http.defaults.headers
defaults.patch = defaults.patch || {}
defaults.patch['Content-Type'] = 'application/json'
create: (attrs) ->
offer = new @service(offer: attrs)
offer.$save ($offer) ->
attrs.id = $offer._id
attrs
现在,当我将服务重命名为 OfferService 时,我没有收到循环依赖的错误。为什么 angular.js 服务会干扰 mongoid 模型?
更新:我刚刚发现,即使我重命名控制器,循环依赖问题也会出现。我忘了取消注释Offer.where(...)
规范的一部分。为什么会发生这种情况,或者让我问一下,我可能搞砸的任何想法?
更新一
我已添加并将其gem 'capybara-angular'
包含spec_helber.rb
在https://github.com/wrozka/capybara-angular中所述。至少测试确实运行知道。不幸的是,我现在遇到了这个问题,因为上面的 gem,evaluate_script 不再被路由到 poltergeist。