Displaying 19 results from an estimated 19 matches for "and_yield".
2007 Aug 12
5
stubbing a method that yeilds sequential results
...ts(attributes)
Connector.each_result(attributes) do |result|
yield result
end
end
end
I''ve worked out how to stub things for the case where the Connector.
each_result method yields a result once
#setup
@result = mock("result")
Connector.stub!(:each_result).and_yield(@result)
@attributes = {}
@results = []
@block = Proc.new { |r| @results << r }
#action
Intermediate.search_results(@attributes, &@block)
# expectation
@results.should == [@search_result]
However, what I actually need to do is check each result that is
yielded by the Connector.each...
2008 Jun 05
1
Using and_yield to pass multiple or no iterations
...ODE>
Stupid code, I know.
I have two questions with it. The first is, would it be possible to set
it up to test the case when each_row operates on an empty Array? Sort of
like this:
<SPEC>
it "should return false if it does not iterate over anything" do
@egg.stub!( each_row ).and_yield( nil ) # I know this doesn''t work
@egg.do_thing.should be_false
end
it "should return true if it does iterate over something" do
@egg.stub!( each_row ).and_yield( :value )
@egg.do_thing.should be_true
end
</SPEC>
Secondly, is this the best (correct) way to pass mult...
2007 Aug 08
5
and_yield + instance_eval(&block)
...# here, self is the instance of Foo
# created by new
end
But normally self is the object in which
Foo.new.bar {...} occurs.
Foo.new.baz do
# self is the execution context
# in which Foo.new was called,
# since a block is a closure
end
The second case is easy; it is covered by and_yield (with no arguments).
Is there some way to spec the first case? Do I smell the need for a
patch?
Scott
2007 Aug 06
1
Stubbing Enumerable#each
I have a mock of an instance of a class which descends from Array:
class ArrayDescendent < Array; end
#... in the specs...
@descendent = mock ArrayDescendent
How would I stub out ArrayDescendent#each, which is inherited from
Array, to return multiple values successively? I could use
and_yield, but that is raising an arity error (the anonymous function/
block should expect only *one value at a time, but and_yield is
yielding both values once). Should I be using a lambda expression here?
Tips are welcome...Thanks,
Scott
2007 Apr 20
7
Stubbing Model.new w/ block?
...ted to be able to stub out Receipt.new so that I could set
expectations on the methods called on the resulting Receipt object.
If in my test setup I do:
@receipt = mock_receipt
Receipt.stub!(:new).and_return(@receipt)
The block part of the code will never be called. If I do:
Receipt.stub!(:new).and_yield(@receipt)
Then @receipt in my model will be set to the return value of the block.
I also tried something along the lines of:
Receipt.stub!(:new) do
yield @receipt
@receipt
end
but got some weird no block error.
Help? Am I going about this all wrong?
-james
--
James A. Hillyerd <jame...
2007 Jan 25
0
mocking methods that receive blocks - back to mocking Thread again
...!(:run)
Thread.stub!(:new)
end
specify "should not run a task in the main thread" do
@task.should_not_receive(:run)
@runner.execute(@task)
end
specify "should run a task in a new thread" do
Thread.should_receive(:new).with(@task).and_yield(@task)
@task.should_receive(:run)
@runner.execute(@task)
end
specify "should be running while a task is beeing executed" do
Thread.should_receive(:new).with(@task).and_yield(@task)
@task.should_receive(:run) do
@runner.should_be_runn...
2008 Dec 04
3
The RSpec way of doing this? Need help on validating block
Hello, I''m back again with more questions about mocks and how to do good
testing in general. Let''s say I''m writing this EmailSender class and I
want to make it totally awesomely tested with RSpec. Here''s how far I''ve
gotten so far:
require ''net/smtp''
class EmailSender
def send_email
2007 Jul 24
2
Mocking Resolv::DNS?
Hello Rspecers,
I have a rails project where I am calling Resolv::DNS.open and then
using the block to check a domain name.
The code snippet in question is:
domain = "mytest.com"
Resolv::DNS.open do |dns|
@mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
end
I obviously want to stub this out, especially for speed but can''t
quite work out how.
I
2007 Apr 17
8
Verifying that a block calls a method
I have something like the following:
def my_fun
my_fun2 do
raise Error
end
end
I know that I can verify that the method receives my_fun2. How can I
mock/stub out the example to verify that it calls raise Error?
Scott
2006 Dec 12
3
Stubs breaking other things
Hi All
I''m working on converting some existing controller specs to use mocks
and stubs rather than real ActiveRecord objects in a Rails project.
In one of my controller actions, I use a database transaction. So,
the obvious thing to do is have this in my setup block:
Project.stub!(:transaction)
Firstly, is there an easy way to have that stub yield to the block
passed to any
2007 Feb 28
12
Specifying that code is called in a block
...I have a section of code like this:
ActiveRecord::Base.transaction do
cow.save!
duck.save!
dog.save!
end
(Names changed to protect the innocent.)
I''d like to specify that the saves run in a transaction. I can do
ActiveRecord::Base.should_receive(:transaction).and_yield
But is there any way to specify that the code is called as above, and
not as
cow.save!
ActiveRecord::Base.transaction do
duck.save!
end
dog.save!
?
Ta
Ashley
2007 Nov 04
3
Specing raising error, handling, and then not raising error
...I thought something like this, but it doesn''t work:
it "should only increase the sent emails counter if the email was sent" do
@smtp = mock(Net::SMTP)
@smtp.should_receive(:send_message).exactly(4).times.and_return(true,
IOError, true, true)
Net::SMTP.stub!(:start).and_yield(@smtp)
@sender = Mailer::Sender.new(valid_args(:emails => @emails))
@sender.sent_emails.should == 3
end
If anyone has any pointers, that would be great.
I am using rSpec trunk - today''s release.
Mikel
2008 Jun 11
3
help with test design
I''m having trouble figuring out how to drive the design of a class.
I''m hoping I can get a hint or two from the more experienced BDDers on
this list.
I''m writing an adapter class that sits between a 3rd party library and
my business logic. I would like to extract some information from
objects available from the 3rd party lib and covert them into ruby
objects.
2008 May 27
2
Help with Writing Meaningful Specs
Hello all,
In yet another attempt to learn Ruby on Rails and Rspec I have started
writing a simple life streaming app with will aggregate feeds from several
places and save them in a database for later use.
An internet search eventually led me to the following method for looping
through the feeds in the database, getting the contents of the URL and then
passing this into another model to prepare
2007 Feb 23
4
How can I spec this? The method gets passed a block...
I''m using Jim Weirich''s Builder library. The code I want to spec is
xml.video do
xml.id @video_id
xml.views @views
xml.date(@date.to_s) if @date
end
I''d like to mock it, rather than asserting that the XML is the right
string. I can do one spec:
specify "should create a video tag" do
@mock_builder.should_receive(:video)
2007 Feb 28
2
Fixture name not available as class variable in spec
Hi there,
I can''t seem to access the fixture name as a class variable from my
specs. I have to set the variable in the spec setup.
... [fixtures]
my_fixture:
id: 1
... [rpsec]
@my_fixture.id.should == 1
... [error]
You have a nil object when you didn''t expect it!
The error occurred while evaluating nil.id
... [my setup]
RSpec-0.7.5.1 (r1395) - BDD for Ruby
Rails
2007 Oct 09
23
Testing layouts with RSpec on Rails
Hey guys,
Does anyone have any wisdom to share on the subject of speccing Rails
layouts?
Most of it''s plain old view specs stuff, but are there sensible ways
to verify things like the yield call? (Mocking doesn''t catch that)
Thanks,
Matt
--
Matt Patterson | Design & Code
<matt at reprocessed org> | http://www.reprocessed.org/
2007 Jul 26
5
Coding standards and whitespace
...Specifications in selected files or
directories.tmCommand
RSpec.tmbundle/info.plist
RSpec.tmbundle/Snippets/and_raise.tmSnippet
RSpec.tmbundle/Snippets/and_return_block.tmSnippet
RSpec.tmbundle/Snippets/and_return_value.tmSnippet
RSpec.tmbundle/Snippets/and_throw.tmSnippet
RSpec.tmbundle/Snippets/and_yield.tmSnippet
RSpec.tmbundle/Snippets/any_number_of_times.tmSnippet
RSpec.tmbundle/Snippets/at_least.tmSnippet
RSpec.tmbundle/Snippets/at_least_once.tmSnippet
RSpec.tmbundle/Snippets/at_least_twice.tmSnippet
RSpec.tmbundle/Snippets/at_most.tmSnippet
RSpec.tmbundle/Snippets/at_most_once.tmSnippet
RSpec....
2007 Aug 31
48
Deprecating the mocking framework?
I saw in one of Dave C.''s comments to a ticket that "our current plan
is to deprecate the mocking framework." I hadn''t heard anything about
that, but then again I haven''t paid super close attention to the list.
Are we planning on dumping the mock framework in favor of using Mocha
(or any other framework one might want to plug in?).
Pat