Globally injecting am AngularJS factory mockup when testing with Jasmine


I'm creating unit tests for my AngularJS application's controllers. In myapp.run I'm injecting and using a factory, called UsersFactory, somehow like this:

myApp.run(['$rootScope','UsersFactory', function ($rootScope,UsersFactory) { UsersFactory.getMe().then(function(data) { //.. }); }]);

I created a Mock for UsersFactory called UsersFactoryMock. It has the same methods as UsersFactory, but the implementation is different (fake) of course.

I would like to inject UsersFactoryMock, in order to be able myApp.run to use it instead of UsersFactory.

I was trying to do so using beforeEach:

beforeEach(module(function ($provide, $injector) { $provide.service("UsersFactory", $injector.get("UsersFactoryMock") ); }));

But running the test, it tells me,


Error: [$injector:modulerr] Failed to instantiate module function ($provide) due to: ReferenceError: UsersFactoryMock is not defined"


How could I achieve my goal?

Thanks in advance.


Does UsersFactoryMock need to be registered with the injector? Why not just provide a plain object as your mock?

var UsersFactoryMock = { getMe: function () { // mocked method } }; beforeEach(module(function ($provide) { $provide.service("UsersFactory", UsersFactoryMock); }));

Moreover, testing code in a run block is generally messy. You should move such logic into a service which can then be tested like any other. See: <a href="https://stackoverflow.com/a/18089426/2943490" rel="nofollow">https://stackoverflow.com/a/18089426/2943490</a>.


  • Custom Failure Handler for withFailureHandler - Google Apps Script
  • Accessing list of playlists from Pager object
  • Change zIndex in HighChart
  • Google Bigquery Command Line Return Limit
  • Hibernate in Glassfish - Ejb3Configuration NoClassDefFoundError
  • How to remove single character words from string with preg_replace
  • Mockery and Laravel constructor injection
  • FTS3 searches in ORMLite?
  • Backward compatibility of Python 3.5 for external modules
  • Appium MobileElement swipe returns unknown server error
  • retrieving data from url in iphone
  • AWS-SES: Handling Bounces for Invalid ISPs
  • Swift Initialization Rule Confusion
  • can variables be set randomly when declaring them again?
  • xtable - background colour of added rows
  • Synchronize windows folders
  • How can I mock ui-router's resolve values when testing a state's configuration?
  • Access user's phone number on iOS 7
  • abstracting over a collection
  • Servlet stops working on Tomcat server after some hits or time
  • Can't remove headers after they are sent
  • Salesforce Different WSDL files and when to use
  • C++ pointer value changes with static_cast
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • Zurb Foundation _global.scss meta styles for js?
  • How to do unit test for HttpContext.Current.Server.MapPath
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • angularjs unit test when to use $rootScope.$new()
  • Incrementing object id automatically JS constructor (static method and variable)
  • Join two tables and save into third-sql
  • How to make Safari send if-modified-since header?
  • Cannot Parse HTML Data Using Android / JSOUP
  • Free memory of cv::Mat loaded using FileStorage API
  • C# - Getting references of reference
  • unknown Exception android
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app