开发者

Rails -- use RSpec "get" and "response" methods in Rake task

开发者 https://www.devze.com 2023-03-21 16:49 出处:网络
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:

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 th开发者_运维百科at, 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?


why do you need to go through the url to do this action with your ContentItem ? Why not just use the local obj and do stuff?

Basically it looks like you're mixing up view-code with model code here... the model's object should not depend on values in the html... if there is some information being figured out in the view... put it into a method on the ContentItem model and call that code either from the view... or from this rake task.

Edit: Ok.... well if you really need to GET a URL - look into Ruby's Net::Http gem - that will literally fetch URLs. Rails doesn't do that as standard... even for local URLs.

You might then be able to use a parser such as hpricot or nokogiri to parse the results to find the tag you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消