使用RSpec测试Datamapper模型 [英] Testing Datamapper models with RSpec
问题描述
我正在使用RSpec测试使用DataMapper的Sinatra应用程序。
以下代码:
它应更新商品的标题做
lambda做
发表 / hello / edit,:params => {
:title => ‘再见’,
:body => 'goodbye world'
}
end。应该更改(Snippet,:title).from('hello')。to('goodbye')
end
导致此错误:
标题应该最初是 hello,但是是#< DataMapper :: Property :: String @ model = Snippet @ name =:title>
我当然可以通过删除lambda并仅检查是否满足以下条件来 hack :
片段。 first.title.should =='再见'
但这不是一个长期的解决方案,因为 .first 代码段将来可能会有所不同。
有人可以告诉我正确的语法吗?
谢谢。
它应该更新item's title do
snippet = Snippet.first(:title => hello)
post / hello / edit,:params => {
:title => ‘再见’,
:body => '再见世界'
}
snippet.reload.title.should =='再见'
结束
感谢@Dan Tao的回答对我有所帮助。
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.
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.
这篇关于使用RSpec测试Datamapper模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!