0

I'm testing a Sinatra application, which is using DataMapper, with RSpec.

The following code:

it "should update the item's title" do
  lambda do
    post "/hello/edit", :params => {
      :title => 'goodbye',
      :body  => 'goodbye world'
    }
  end.should change(Snippet, :title).from('hello').to('goodbye')
end

Results in this error:

title should have initially been "hello", but was #<DataMapper::Property::String @model=Snippet @name=:title>

I can of course hack this by removing the lambda and only checking if:

Snippet.first.title.should == 'goodbye' 

But that can't be a long term solution since the .first Snippet may not be the same in the future.

Can someone show me the right syntax?

Thanks.

Samy Dindane
  • 17,900
  • 3
  • 40
  • 50

2 Answers2

2

Your spec as written implies that the lambda should actually change the value of the class attribute Snippet.title; I think what you want is something like this:

it "should update the item's title" do
  snippet = Snippet.first(:title => "hello")
  lambda do
    post "/#{snippet.title}/edit", :params => {
      :title => 'goodbye',
      :body  => 'goodbye world'
    }
  end.should change(snippet, :title).from('hello').to('goodbye')
end

Right?

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • That's logical, thanks. It unfortunately didn't work as `snippet` remains the same variable with the same content. I tried using `snippet.reload`, but it doesn't work inside of the `should change`. – Samy Dindane May 08 '12 at 19:26
1

I finally fixed it using:

it "should update the item's title" do
  snippet = Snippet.first(:title => "hello")
  post "/hello/edit", :params => {
    :title => 'goodbye',
    :body  => 'goodbye world'
  }
  snippet.reload.title.should == 'goodbye'
end

Thanks to @Dan Tao whose answer helped me.

Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
  • If @Dan answer helped you, why didn't you voted it for the correct answer, and edited you question to bring up the final solution? – lcguida Jun 04 '14 at 22:09
  • I didn't feel like changing most of his code without his permission. Maybe I should have asked him. – Samy Dindane Jun 06 '14 at 13:12