Testing Datamapper models with RSpec

China☆狼群 提交于 2019-12-25 05:29:13

问题


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.


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/10504505/testing-datamapper-models-with-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!