使用RSpec测试Datamapper模型

我正在使用RSpec测试一个使用DataMapper的Sinatra应用程序。

以下代码:

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 

导致此错误:

title应该最初是“hello”,但是#DataMapper :: Property :: String @ model = Snippet @ name =:title>

我当然可以通过删除lambda来解决这个问题,并且只检查是否:

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

但这不是一个长期解决方案,因为.first Snippet在未来可能不一样。

有人能告诉我正确的语法吗?

谢谢。

你写的规范暗示lambda实际上应该改变属性Snippet.title的值; 我想你想要的是这样的:

 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 

对?

我终于修复了它:

 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 

感谢@Dan Tao的回答帮助了我。