1

I know RSpec has useful methods "get" and "response.should" to run integration tests - I want to know how I can use these (or other methods to achieve the same result) in a Rake task:

desc "Check all content items with type 0 and do something"
task :my_task => :environment do
    ContentItem.where("content_type = ?", 0).each do |obj|
        get "/my_path/"+obj.value
        if (response has a certain html tag)
            perform some action on obj
        end        
    end
end

I know that I can't just run rspec methods like that, but this is effectively what I need to do, and I need to be able to process information returned when /my_path/obj.value is opened. Does anyone have any suggestions?

4

1 回答 1

0

为什么您需要通过 url 对您的 ContentItem 执行此操作?为什么不直接使用本地 obj 并做一些事情呢?

基本上看起来你在这里混合了视图代码和模型代码......模型的对象不应该依赖于 html 中的值......如果在视图中发现了一些信息......把它放入ContentItem模型上的一个方法,并从视图中调用该代码......或从这个 rake 任务。

编辑:好的....好吧,如果你真的需要获取一个 URL - 查看 Ruby 的 Net::Http gem - 这将真正获取 URL。Rails 并没有将其作为标准......即使对于本地 URL。

然后,您可以使用诸如 hpricot 或 nokogiri 之类的解析器来解析结果以找到您需要的标签。

于 2011-07-18T17:26:00.270 回答