You should probably just change that spec. The element that you want
to test is a proc (you hope) getting invoked inside of the subject
block, which is a weird pattern, and you don't need the #let blocks
really, since you only use those objects once.
Try:
describe "#count!" do
specify "when a loan exists from giver to receiver" do
loan = Loan.gen
money_flow = MoneyFlow.gen(:giver => loan.loaner, :receiver =>
loan.loanee, :currency => loan.currency)
expect do
Loan.count!(money_flow)
end.to_not change(Loan, :count)
end
end
I'm assuming you are using rspec 2, though it will work in rspec 1.x
with a syntax change. If you still get that error, post the new
backtrace here. At the very least the spec and the backtrace will be
more meaningful this way, and may be easier to debug (I hope).
On Jan 10, 10:42 am, Zhi-Qiang Lei <[email protected]> wrote:
> Hi,
>
> I have some code as follow.
>
> class Loan
> include DataMapper::Resource
> belongs_to :loaner, Person, :key => true
> belongs_to :loanee, Person, :key => true
> property :currency, Enum[*CURRENCY_CODES], :key => true
> property :amount, Decimal, :scale => 2, :default => 0
>
> def self.count!(money_flow)
> loan = get(money_flow.currency, money_flow.giver_id,
> money_flow.receiver_id)
> loan.amount += money_flow.amount
> end
>
> # more code...
> end
>
> class MoneyFlow
> include DataMapper::Resource
> belongs_to :giver, Person
> belongs_to :receiver, Person
>
> property :id, Serial
> property :amount, Decimal, :scale => 2, :min => 0.01
> property :currency, Enum[*CURRENCY_CODES], :default => :CNY
>
> # more code...
> end
>
> When I test the "count!" class method as follow:
>
> describe "#count!" do
> subject { lambda { Loan.count!(money_flow) } }
> context "when a loan exists from giver to receiver" do
> let!(:loan) { Loan.gen }
> let(:money_flow) { MoneyFlow.gen(:giver => loan.loaner, :receiver =>
> loan.loanee, :currency => loan.currency) }
> it { should_not change(Loan, :count) }
> end
> end
>
> It tells me I got a Immutable Error. I feel strange on that. They have keys,
> and I didn't destroy the record. Can you see why? Thanks.
>
> 1) Loan#count! when a loan exists from giver to receiver
> Failure/Error: subject { lambda { Loan.count!(money_flow) } }
> DataMapper::ImmutableError:
> Immutable resource cannot be modified
> # ./lib/models.rb:49:in `count!'
> # ./spec/models_spec.rb:59:in `block (4 levels) in <top (required)>'
> # ./spec/models_spec.rb:63:in `block (4 levels) in <top (required)>'
>
> On Jan 10, 2011, at 11:34 PM, Ted Han wrote:
>
>
>
>
>
> > Objects are immutable when they've been deleted or frozen for some reason.
> > Deleting a record from your data store will always result in that record
> > being frozen.
>
> > Is there a specific problem that you're having?
>
> > -T
>
> > On Mon, Jan 10, 2011 at 10:23 AM, Zhi-Qiang Lei <[email protected]>
> > wrote:
> > Dear All,
>
> > Could anyone answer me when a resource should be immutable? And why? Thanks.
>
> > Best regards,
> > Zhi-Qiang Lei
> > [email protected]
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "DataMapper" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group
> > athttp://groups.google.com/group/datamapper?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "DataMapper" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group
> > athttp://groups.google.com/group/datamapper?hl=en.
>
> Best regards,
> Zhi-Qiang Lei
> [email protected]
--
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en.