stub

How to stub require() / expect calls to the “root” function of a module?

 ̄綄美尐妖づ 提交于 2019-12-22 04:13:10
问题 Consider the following jasmine spec: describe("something.act()", function() { it("calls some function of my module", function() { var mod = require('my_module'); spyOn(mod, "someFunction"); something.act(); expect(mod.someFunction).toHaveBeenCalled(); }); }); This is working perfectly fine. Something like this makes it green: something.act = function() { require('my_module').someFunction(); }; Now have a look at this one: describe("something.act()", function() { it("calls the 'root' function

Overriding functions in other modules in node.js

被刻印的时光 ゝ 提交于 2019-12-21 20:47:54
问题 I'm trying to stub a function with nodeunit in a Node.js app. Here's a simplified version of what I'm trying to do: In lib/file.js : var request = require('request'); var myFunc = function(input, callback){ request(input, function(err, body){ callback(body); }); }; In test/test.file.js : var file = require('../lib/file'); exports['test myFunc'] = function (test) { request = function(options, callback){ callback('testbody'); }; file.myFunc('something', function(err, body){ test.equal(body,

Is there a way to undo Mocha stubbing of any_instance in Test::Unit

时光总嘲笑我的痴心妄想 提交于 2019-12-21 06:49:27
问题 Much like this question, I too am using Ryan Bates's nifty_scaffold. It has the desirable aspect of using Mocha's any_instance method to force an "invalid" state in model objects buried behind the controller. Unlike the question I linked to, I'm not using RSpec, but Test::Unit. That means that the two RSpec-centric solutions there won't work for me. Is there a general (ie: works with Test::Unit) way to remove the any_instance stubbing? I believe that it's causing a bug in my tests, and I'd

RSpec: how to stub inherited method current_user (w/o Devise)?

廉价感情. 提交于 2019-12-21 03:13:34
问题 I have a controller based on MHartl's RoR4 Tutorial And just like MHartl, I'm not using Devise , I rolled my own authentication system Having trouble with the RSpec for UsersController#Edit since the view has a call to current_user.admin? and the controller calls @path_switch = path_switch I keep getting RSpec errors along the lines of: 1) User Pages Edit Failure/Error: visit edit_user_path(user) NoMethodError: undefined method `admin?' for nil:NilClass # ./app/controllers/users_controller.rb

What is the difference between mocks, stubs and fake objects [closed]

假如想象 提交于 2019-12-20 12:03:49
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . Although there are plenty of resources, even here on SO, only two of the terms are compared to each other in these Q/A. So, in short, what is each one of them? And how they all relate to each other? Or don't they at all? 回答1: Difference between mock and stub is very simple - mock can make your test fail, while

Can RSpec stubbed method return different values in sequence?

南楼画角 提交于 2019-12-20 09:09:26
问题 I have a model Family with a method location which merges the location outputs of other objects, Members. (Members are associated with families, but that's not important here.) For example, given member_1 has location == 'San Diego (traveling, returns 15 May)' member_2 has location == 'San Diego' Family.location might return 'San Diego (member_1 traveling, returns 15 May)' The specifics are unimportant. To simplify the testing of Family.location, I want to stub Member.location. However, I

Android Local Service Sample, bindservice(), and ServiceConnection()

本秂侑毒 提交于 2019-12-19 04:18:29
问题 I have a question which is related to this question that was asked by @mnish about a year ago. Please have a look at his question and code. He implements a ServiceConnection() and passes it to bindService(). This follows the Local Service Sample in the Service documentation near the top. I want to implement the Local Service Sample, so I am trying to add some details from @mnish question/answer. In ServiceConnection() @mnish has this line that confuses me: mService = ILocService.Stub

node express es6 sinon stubbing middleware not working

﹥>﹥吖頭↗ 提交于 2019-12-19 03:36:33
问题 I am writing the mocha unit test for the my express router. I found that however I try to stub the middleware, it still execute the middleware code. Here is my router & test, could anyone figure out? Router: import { aMiddleware, bMiddleware, cMiddleware } from '../middleware.js'; router.post('/url', aMiddleware, bMiddleware, cMiddleware, function(req, res) { ... } Middleware: AuthMiddleware.aMiddleware = async (req, res, next) => { console.log('in real middleware'); next(); } Test: var

When to Expect and When to Stub?

心不动则不痛 提交于 2019-12-18 12:23:48
问题 I use NMock2, and I've drafted the following NMock classes to represent some common mock framework concepts: Expect : this specifies what a mocked method should return and says that the call must occur or the test fails (when accompanied by a call to VerifyAllExpectationsHaveBeenMet() ). Stub : this specifies what a mocked method should return but cannot cause a test to fail. So which should I do when? 回答1: A lot of mocking frameworks are bringing the concepts of mocks & stubs closer & closer

Django unit testing with date/time-based objects

≡放荡痞女 提交于 2019-12-18 10:33:49
问题 Suppose I have the following Event model: from django.db import models import datetime class Event(models.Model): date_start = models.DateField() date_end = models.DateField() def is_over(self): return datetime.date.today() > self.date_end I want to test Event.is_over() by creating an Event that ends in the future (today + 1 or something), and stubbing the date and time so the system thinks we've reached that future date. I'd like to be able to stub ALL system time objects as far as python is