Displaying 20 results from an estimated 3000 matches similar to: "How to test for exceptions"
2007 Oct 26
5
specing rescue, ensure and else blocks of an Exception
Greetings,
I''m using rspec with rcov for my applications and there is one issue
which I cannot solve nor can find any proper information regarding
it: specing what is in a rescue block in case of an exception.
I''m using Ruby on Rails and I usually make use of exceptions in my
controllers, like the following example:
def action
@foo = Foo.find(1)
2007 Nov 17
7
Down with Lambda!!
Rspec is all about using natural language to define behavior. In this
context, I feel that lambda is sorely out of place. I was chatting on
#irc and a pal of mine (wycats) proposed an interesting alternative:
alias_method :doing, :lambda
so instead of something like
lambda {post :create, {:title => nil}}.should
raise_error(ActiveRecord::RecordInvalid)
we get
doing {post :create, {:title
2006 Nov 22
3
Controller Isolation
Hi,
I have a project that I had running under rspec 0.6.x and recently upgraded
to 0.7.2.
I am trying to isolate my controllers from the database as I go through and
change all the specs to run under 0.7.2.
I am having a problem where I need to make the create! method return the
mocked object as well as raise RecordInvalid exception. Is this at all
possible?
I use the rescue statement in my
2007 Aug 23
6
controller spec with model that validates_uniqueness
I want to use mocks and stubs to test the controller, but am having
trouble getting my validation not to trigger. Here''s the code:
# spec:
Image.stub!(:find).and_return(@image)
@image.should_receive(:save!).once.with(:any_args)
put :update, :id => @image.id, :category_id =>
@category.id, :image => {:name => ''test'', :image_number =>
2011 Mar 04
5
How to intercept an instance method from StdLib (1.9.2)
Hi,
I''m struggling with something that seems to be simple, and I''ve not
had any joy following the RSpec books suggestions (p. 187).
I''d like to test that a method raises and error when a file is not found.
I''ve tried adding this in my example just before I call my method, but
it never seems to get invoked.
Pathname.stub(:exist?).and_return(false)
The whole
2007 Sep 13
2
Failing to raise an exception in a stub
I''ve come across rather strange behaviour when trying to raise an
exception in a stubbed method.
I''m speccing the behaviour of a Rails create action, where I''m using
save! to catch failed saves. In the case of working save, I''m using
the following stub:
@client.stub!(:save!).and_return(true)
which works fine.
However, in the negative case,
2007 Oct 26
7
Weird failing spec
Hi guys,
I have a weird failing spec, for which I just cannot figure out the
reason of failure. I''m now rewriting my controller specs based on the
advice of David and Ashley, and I got stuck on this (see: [rspec-
users] specing rescue, ensure and else blocks of an Exception).
http://pastie.caboo.se/111221
I''ve tried everything, like stubbing out :update_attributes! , even
2007 Aug 08
5
Can''t seem to spec a ActiveRecord::RecordInvalid exception properly...
1 def create
2 @user = User.new(params[:user])
3 @user.save!
4 self.current_user = @user
5 redirect_to user_path(@user)
6 flash[:notice] = "Thanks for signing up!"
7 rescue ActiveRecord::RecordInvalid
8 render :action => ''new''
9 end
I can''t seem to properly spec this out. I am trying numerous things, the
latest one is
2007 Jan 30
5
errors while testing resource controller using rpec
I am testing a resource called venue in this piece of code (generated
using script/rspec_resource)
====================
context "Requesting /venues using POST" do
controller_name :venues
setup do
@mock_venue = mock(''Venue'')
@mock_venue.stub!(:save).and_return(true)
@mock_venue.stub!(:to_param).and_return(1)
Venue.stub!(:new).and_return(@mock_venue)
2006 Mar 19
2
Functional Testing
Hey all,
I have a many-to-many relationship (Questions
has_and_belongs_to_many Answers), and when I destroy
the Question (the one) I also destroy all the Answers
(the many).
Since the following doesn''t seem to work for
has_and_belongs_to_many:
class Question < AR::Base...
has_and_belongs_to_many :answers, :dependent =>
:destroy
end
I have this embedded in a transaction block
2007 May 10
5
Shared behavior and_raise problem, and site aestetics
Hello,
Is anyone else having trouble with the and_raise method on mocks,
with respect to shared behaviors? If I do the following instead:
foo.should_receive(:bar).and_return { raise }
everything seems to work. I have been supplying no arguments to
and_raise, by the way.
Also, you may want to make the background in the code examples on the
home page a little lighter. I am hardly able to
2007 Feb 13
16
Error against latest trunk while testing via spec for model
Hi
I just did an update to lates trunk
=================
context "Given a generated venue_spec.rb with fixtures loaded" do
fixtures :venues
specify "fixtures should load two Venues" do
Venue.should have(2).records
end
end
==================
gives me
==========
1)
TypeError in ''Given a generated venue_spec.rb with fixtures loaded
fixtures should load two
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
2008 May 20
5
How to write a test for validates_uniqueness_of
Hi,
I have a spec
it "should have a unique username "
I have a code:
validates_uniqueness_of :user_name
Now, I don''t know how to test this code. In order to test this, do I
need to run `save`?
For example,
@user = User.create(:username => "mike")
@another = User.create(:username => "mike")
@another.save.should be_false
This messes up test
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.
2007 Nov 28
6
Newbie question
I installed Rspec and am getting the following failure:
$ sudo gem install rspec
Successfully installed rspec-1.0.8
Installing ri documentation for rspec-1.0.8...
Installing RDoc documentation for rspec-1.0.8...
$ spec -v
RSpec-1.0.8 (r2338) - BDD for Ruby
http://rspec.rubyforge.org/
$ cat acct.rb
describe Account, " when first created" do
it "should have a balance of $0"
2008 Jan 30
9
catching errors, rspec basics
Trying to spec the following but don''t know if I''m using the right matcher.
How do I spec? Plz, sugar on tops.
Audience.stats
- should have a stats of 80 when passed a flux of 10
- should return an error when passed a string (ERROR - 1)
1)
TypeError in ''Audience.stats should return an error when passed a string''
String can''t be coerced into Fixnum
2007 Sep 20
4
alias :calling :lambda
Sprinkling my examples with ''lambda'' has always seemed like a bit of a
wart to me. I''ve gotten into the habit of adding ''alias :calling
:lambda'' to my spec suites. My examples then look like:
calling { Foo }.should raise_error
calling { Bar }.should_not raise_error
Is there a reason that RSpec core has chosen not to make exception
expectations more
2009 Jul 04
12
save! not allowed after validates_uniqueness
I have this in a model
validates_uniqueness_of :aspect, :scope => :user_id
In an instance method of the same model I have "save!" but I don''t touch
the :aspect attribute in that instance method at all.
Whenever that save! command is run however I get this error:
ActiveRecord::RecordInvalid: Validation failed: Aspect has
already been taken
I don''t
2011 Sep 01
2
Custom ActiveRecord Error Message
I''m generating 3 models on 1 controller and i''m rescuing
ActiveRecord::RecordInvalid for validation errors, however 2 of the
models have the same attribute called "name", and if there''s an error on
that field, I wouldn''t know whether that belongs to model A or model B.
What then is the best way of modifying the error message of a validation
error