stub

Stubbing Chained Queries in Rails 3 and Rspec

馋奶兔 提交于 2019-11-30 06:46:58
I'm trying to test a scope I have that is based upon a chain of other scopes. ("public_stream" below). scope :public, where("entries.privacy = 'public'") scope :completed, where("entries.observation <> '' AND entries.application <> ''") scope :without_user, lambda { |user| where("entries.user_id <> ?", user.id) } scope :public_stream, lambda { |user| public.completed.without_user(user).limit(15) } Using a test like this: it "should use the public, without_user, completed, and limit scopes" do @chain = mock(ActiveRecord::Relation) Entry.should_receive(:public).and_return(@chain) @chain.should

rspec: How to stub an instance method called by constructor?

喜夏-厌秋 提交于 2019-11-30 06:24:22
问题 class A def initialize @x = do_something end def do_something 42 end end How can I stub do_something in rspec, before the original implementation is called (thus assigning 42 to @x )? And without changing the implementation, of course. 回答1: Here's the commit which adds the feature to rspec - This was on May 25 2008. With this you can do A.any_instance.stub(do_something: 23) However, the latest gem version of rspec (1.1.11, October 2008) doesn't have this patch in it. This ticket states that

how to stub HttpControllerContext

我的梦境 提交于 2019-11-30 05:18:54
I am trying to unit-test a piece of code that gets called from a WebAPI (OData) controller and takes in an HttpControllerContext: public string MethodToTest(HttpControllerContext context) { string pub = string.Empty; if (context != null) { pub = context.Request.RequestUri.Segments[2].TrimEnd('/'); } return pub; } To Unit-test this i need an HttpControllerContext object. How should i go about it? I was initially trying to stub it with Microsoft Fakes, but HttpControllerContext doesnt seem to have an interface (why??), so thats doesnt seem to be an option. Should i just new up a new

How to stub/mock JDBC ResultSet to work both with Java 5 and 6?

人走茶凉 提交于 2019-11-30 04:37:29
问题 I'm testing some of my classes working with JDBC statements etc and now I got problem with JDBC ResultSet interface: The software should run both with Java 5 and Java 6 and hence the tests should also be run with both versions. Unfortunately Java 6 has introduced a bunch of new methods (which is still not a big deal) that return a bunch of new classes/interfaces, which makes the things more difficult. (see Frank Carver’s Punch Barrel - Java 6 breaks JDBC for example) Before finding out these

Django unit testing with date/time-based objects

↘锁芯ラ 提交于 2019-11-29 22:20:14
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 concerned. This includes datetime.date.today() , datetime.datetime.now() , and any other standard date

Stubbing Chained Queries in Rails 3 and Rspec

不羁的心 提交于 2019-11-29 06:38:19
问题 I'm trying to test a scope I have that is based upon a chain of other scopes. ("public_stream" below). scope :public, where("entries.privacy = 'public'") scope :completed, where("entries.observation <> '' AND entries.application <> ''") scope :without_user, lambda { |user| where("entries.user_id <> ?", user.id) } scope :public_stream, lambda { |user| public.completed.without_user(user).limit(15) } Using a test like this: it "should use the public, without_user, completed, and limit scopes" do

Testing software: fake vs stub

老子叫甜甜 提交于 2019-11-29 06:10:42
问题 There are quite a few written about stub vs mocks, but I can't see the real difference between fake and stub. Can anyone put some light on it? 回答1: I assume you are referring to the terminology as introduced by Meszaros. Martin Fowler does also mentions them regularly. I think he explains the difference pretty well in that article. Nevertheless, I'll try again in my own words :) A Fake is closer to a real-world implementation than a stub. Stubs contain basically hard-coded responses to an

how to stub HttpControllerContext

耗尽温柔 提交于 2019-11-29 02:54:28
问题 I am trying to unit-test a piece of code that gets called from a WebAPI (OData) controller and takes in an HttpControllerContext: public string MethodToTest(HttpControllerContext context) { string pub = string.Empty; if (context != null) { pub = context.Request.RequestUri.Segments[2].TrimEnd('/'); } return pub; } To Unit-test this i need an HttpControllerContext object. How should i go about it? I was initially trying to stub it with Microsoft Fakes, but HttpControllerContext doesnt seem to

RSpec: Stub chains with arguments?

坚强是说给别人听的谎言 提交于 2019-11-28 22:36:55
Just wondering if/how arguments can be passed in rspec stub chains. To give an example, suppose I have the following action: def index @payments = Payment.order(:updated_at).where(:paid => true) @bad_payments = Payment.order(:some_other_field).where(:paid => false) end In my controller spec, I'd like to be able to stub out both methods and return different results. If only the @payments field were in the action I'd use something like Payment.stub_chain(:order, :where) { return_this } But of course, that will return the same value for @bad_payments . So - in short, how do I include the :updated

EntityFunctions.TruncateTime and unit tests

好久不见. 提交于 2019-11-28 21:09:06
I'm using System.Data.Objects.EntityFunctions.TruncateTime method to get date part of a datetime in my query: if (searchOptions.Date.HasValue) query = query.Where(c => EntityFunctions.TruncateTime(c.Date) == searchOptions.Date); This method (I believe the same applies to other EntityFunctions methods) cannot be executed outside of LINQ to Entities. Executing this code in a unit test, which effectively is LINQ to objects, causes a NotSupportedException to be thrown: System.NotSupportedException : This function can only be invoked from LINQ to Entities. I'm using a stub for a repository with