首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Action有效,但test无效(Shoulda)

Action有效,但test无效(Shoulda)
EN

Stack Overflow用户
提问于 2010-06-12 22:47:25
回答 1查看 157关注 0票数 1

我尝试用下面的代码在Rails中测试我的更新操作:

代码语言:javascript
复制
context "on PUT to :update" do
  setup do
    @countdown = Factory(:countdown)
    @new_countdown = Factory.stub(:countdown)
    put :update, :id => @countdown.id, :name => @new_countdown.name, :end => @new_countdown.end
  end

  should_respond_with :redirect
  should_redirect_to("the countdowns view") { countdown_url(assigns(:countdown)) }
  should_assign_to :countdown
  should_set_the_flash_to /updated/i

  should "save :countdown with new attributes" do
    @countdown = Countdown.find(@countdown.id)
    assert_equal @new_countdown.name, @countdown.name
    assert_equal 0, (@new_countdown.end - @countdown.end).to_i
  end    
end

当我实际使用构建的scaffold完成更新过程时,它可以很好地更新记录,但测试给出了这个错误:

代码语言:javascript
复制
  1) Failure:
test: on PUT to :update should save :countdown with new attributes. (CountdownsControllerTest)
[/test/functional/countdowns_controller_test.rb:86:in `__bind_1276353837_121269'
 /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
 /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: on PUT to :update should save :countdown with new attributes. ']:
<"Countdown 8"> expected but was
<"Countdown 7">.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-19 10:13:46

我本以为那些end专栏会搞砸ruby,但看起来并没有...?

不管怎样,我假设/test/functional/countdowns_controller_test.rb:86就是这个断言:assert_equal @new_countdown.name, @countdown.name

所以你的断言是问@new_countdown.name和@countdown.name是一样的吗?打开工厂看看,我想这是个简单的答案。不确定您在这里尝试测试的是什么。

另外,为什么@countdown = Countdown.find(@countdown.id)使用相同的实例变量名?设置中的@countdown与测试中find行中的@countdown不是相同的吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3029028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档