0

我是 rspec 和 ruby​​ 的新手...

我在 course_rspec.rb 之一中有以下代码

...它“启用课程后检查活动课程” do course = Course.create(:title => "Testing", :description => "Testing") course.enabled = true course.save

active_courses = Course.where(:enabled => true)
active_courses.length.should eql 1

结尾 ...

我收到以下错误

1)启用课程失败/错误后对活动课程的课程检查:active_courses.length.should eql 1

   expected: 1
        got: 0

   (compared using eql?)
 # ./course_spec.rb:86:in `block (2 levels) in <top (required)>'

为什么 Course.where 方法不返回任何对象?当我在 Rails 控制台中执行此操作时,它工作正常。

谢谢你的帮助。

4

1 回答 1

0

您是在test模式下还是在development模式下运行 rails 控制台?请记住,用于测试和开发应用程序的 rails 使用不同的数据库。确保在测试之前准备好所有记录并将其存储在数据库中。为此,您可以使用固定装置或工厂(factory_girl例如)。

附言。相反active_courses.length.should eql 1,您可以使用active_courses.should have(1).item哪个更具可读性。

于 2012-05-28T21:33:52.213 回答