Last active
August 29, 2015 14:04
-
-
Save shicholas/5c755ba5c2e1af65d9ac to your computer and use it in GitHub Desktop.
Feature tests with evaluated Angular Factories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This method returns the value from a factory that uses ngResource and a | |
# method on that factory using Capybara's evaluate_script API. This method | |
# has been tested with Poltergeist, a PhantomJS driver for Capybara | |
# | |
# This method uses window variables because a multi-line javascript function | |
# in Poltergeist must be defined in an anonymous function. Therefore, the only | |
# way I know of to get a closure outside of this context is by defining it | |
# on the window. | |
module AngularFactoryHelper | |
def invoke_factory(factory, method, parameters={}, postData='') | |
page.evaluate_script <<-SCRIPT.strip.gsub(/\s+/,' ') | |
(function resolveResource() { | |
if (!window.#{factory}) { | |
window.#{factory} = angular.element(document.body).injector().get('#{factory}').#{method}(#{parameters}, #{postData + ',' unless postData.empty?} function(data) { window.#{factory} = data }); | |
} else if (!window.#{factory}.$resolved) { | |
resolveResource(); | |
} | |
})() | |
SCRIPT | |
page.evaluate_script "window.#{factory}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#= require angular | |
#= require angular-route | |
#= require angular-resource | |
#= require_self | |
angular | |
.module('appFactories', ['ngResource']) | |
.factory 'Posts', ($resource) -> | |
$resource '/posts.json' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>DockerDemo</title> | |
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> | |
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> | |
<%= csrf_meta_tags %> | |
</head> | |
<body ng-app='appFactories'> | |
<%= yield %> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'features_spec_helper' | |
feature 'Ng Post Factory', :js do | |
# left off the url because it can change | |
let(:expected_json) do | |
'[{"id":1,"title":"Yes","description":"No"' | |
end | |
scenario 'Post should make the proper hits to the app' do | |
Post.create!(title: 'Yes', description: 'No') | |
visit root_path | |
data = invoke_factory('Posts', 'get') | |
expect(data.fetch('promise')).to eq {} | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'features_spec_helper' | |
feature 'Ng Post Factory', :js do | |
# left off the url because it can change | |
let(:expected_json) do | |
'[{"id":1,"title":"Yes","description":"No"' | |
end | |
scenario 'Post should make the proper hits to the app' do | |
Post.create!(title: 'Yes', description: 'No') | |
visit root_path | |
data = invoke_factory('Posts', 'get') | |
expect(data.fetch('promise')).to eq {} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment