Hi all !!
I have this code:
require ''account''
describe Account, "when first created" do
before do
@account = Account.new
end
it "should have a balance of $0" do
@account.balance.should eql(Money.new(0, :dollars))
end
after do
@account = nil
end
end
How can I pass the @account.balance.should eql(Money.new(0, :dollars))
spec. From my understanding you .eql? in ruby is true if the object
are the same (reference points to same object) or if one inherits the
other.
I thought of using this code for Account :
class Account
attr_accessor :balance
def initialize
self.balance = Money.new(0, :dollars)
end
end
class Money < Account
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
end
But still can make it work. It does not pass the spec.
Thanks !
--
Federico Brubacher
www.fbrubacher.com
Colonial Duty Free Shop
www.colonial.com.uy
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/rspec-users/attachments/20070514/a6d01241/attachment.html
On 5/14/07, Federico Brubacher <fbrubacher at gmail.com> wrote:> > Hi all !! > > > I have this code: > require ''account'' > > > describe Account, "when first created" do > before do > @account = Account.new > end > > > it "should have a balance of $0" do > @account.balance.should eql(Money.new(0, :dollars)) > end > > > after do > @account = nil > end > end > > > How can I pass the @account.balance.should eql(Money.new(0, :dollars)) > spec. From my understanding you .eql? in ruby is true if the object > are the same (reference points to same object) or if one inherits the > other.This is only true for certain classes in the Ruby libraries, but not for the Ruby language. By default, equal?, eql? and even == only return true for the same Object. Here''s evidence: irb(main):001:0> class Arbitrary irb(main):002:1> def initialize irb(main):003:2> @member = 1 irb(main):004:2> end irb(main):005:1> end => nil irb(main):006:0> a = Arbitrary.new => #<Arbitrary:0x857b4 @member=1> irb(main):007:0> a2 = Arbitrary.new => #<Arbitrary:0x81b3c @member=1> irb(main):008:0> a == a2 => false irb(main):009:0> a.eql?(a2) => false irb(main):010:0> a.equal?(a2) => false irb(main):011:0> a == a => true irb(main):012:0> a.eql?(a) => true irb(main):013:0> a.equal?(a) => true> > > I thought of using this code for Account : > > class Account > attr_accessor :balance > def initialize > self.balance = Money.new(0, :dollars) > end > end > > class Money < Account > attr_accessor :amount, :currency > def initialize(amount, currency) > @amount = amount > @currency = currency > end > end>From an OO perspective this is probably not what you want. Account andMoney are different concepts and don''t belong in any sort of hierarchical relationship together. This really is a Ruby question, not an RSpec question. I''d recommend that you simply override eql? or == (or both) on your Money class. Do that spec-first separately. Then the example you provided above should pass. Please let us know if that works for you. Cheers, David ps - there''s no need to have after(:each) { @account = nil }. Each example gets run in a new instance of Object, so instance variables are out of scope from example to example.> > > But still can make it work. It does not pass the spec. > Thanks ! > -- > Federico Brubacher > www.fbrubacher.com > > Colonial Duty Free Shop > www.colonial.com.uy > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
I have a bunch of little toggler methods in controllers that look
sorta like this:
# This method is only intended to toggle the approved attribute
via Ajax
def toggle_approved
if request.xhr?
post = Post.find(params[:id])
post.toggle!(''approved'') if post
end
render :nothing => true
end
There''s essentially nothing here to spec other than:
it "should very silently change the state of the
''approved'' field"
But AR takes care of that... and because I''m not exposing the
intermediary post variable as an instance variable, I can''t even see
whether I did anything without going back and querying the database.
That feels like stuff I shouldn''t be doing in a spec. The other side
of the coin is that a spec should describe the functionality of the
controller and one component of that functionality is that it can
toggle the state of the ''approved'' field.
So the conundrum is this: Do I rewrite the code to make it more
verifiable, changing post to @post? Do I actually take before and
after snapshots of the ''approved'' field? Do I trust AR to do
the
right thing and stop obsessing?
Any thoughts?
Mock objects are great for this!
it "should very silently change the state of the
''approved'' field" do
post = mock(''post'')
post.should_receive(:toggle!).with(''approved'')
Post.should_receive(:find).with("1").and_return(post)
xhr post :toggle_approved, :id => 1
end
On 5/15/07, s.ross <cwdinfo at gmail.com> wrote:> I have a bunch of little toggler methods in controllers that look
> sorta like this:
>
> # This method is only intended to toggle the approved attribute
> via Ajax
> def toggle_approved
> if request.xhr?
> post = Post.find(params[:id])
> post.toggle!(''approved'') if post
> end
> render :nothing => true
> end
>
> There''s essentially nothing here to spec other than:
>
> it "should very silently change the state of the
''approved'' field"
>
> But AR takes care of that... and because I''m not exposing the
> intermediary post variable as an instance variable, I can''t even
see
> whether I did anything without going back and querying the database.
> That feels like stuff I shouldn''t be doing in a spec. The other
side
> of the coin is that a spec should describe the functionality of the
> controller and one component of that functionality is that it can
> toggle the state of the ''approved'' field.
>
> So the conundrum is this: Do I rewrite the code to make it more
> verifiable, changing post to @post? Do I actually take before and
> after snapshots of the ''approved'' field? Do I trust AR to
do the
> right thing and stop obsessing?
>
> Any thoughts?
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>