简体   繁体   English

如何在Minitest中测试rake任务?

[英]How to test rake task in Minitest?

I have a rake task in my rails application that published jobs to facebook. 我的Rails应用程序中有一个rake任务,该任务将作业发布到了Facebook。 And then changes some model values. 然后更改一些模型值。 So one way to test the rake task is to invoke the rake task and check values that have been changed. 因此,测试rake任务的一种方法是调用rake任务并检查已更改的值。

test 'z' do
    # setup some data
    Rake::Task['job:publish_to_facebook'].invoke
    Rake::Task['job:publish_to_facebook'].reenable
    # assert table values that has been changed. 
end

But how I can test whether jobs are successfully published on facebook? 但是我如何测试工作是否在Facebook上成功发布? Is there any better strategy except using capybara and selenium-webdriver ? 除了使用capybaraselenium-webdriver之外,还有其他更好的策略吗?

Even if i use stubbing and mocking then how can i verify that my jobs are published on facebook? 即使我使用存根和嘲笑,也该如何验证我的工作在Facebook上发布?

Most tests should not contact an external API, mainly because it will slow down tests and you might also run into rate limits. 大多数测试都不应联系外部API,主要是因为它会减慢测试速度,并且您可能还会遇到速率限制。

Even if i use stubbing and mocking then how can i verify that my jobs are published on facebook? 即使我使用存根和嘲笑,也该如何验证我的工作在Facebook上发布?

The point of stubbing and mocking is precisely not to publish to Facebook. 存根和嘲笑的目的恰恰是发布到Facebook。 Instead, you would create a class called Facebook (for example) with a method like def post_message(message) . 相反,您可以使用诸如def post_message(message)类的方法创建一个名为Facebook的类。 This is the app's front door to Facebook, all calls to Facebook go through this class. 这是该应用程序通往Facebook的大门,所有对Facebook的呼叫均会通过此类。 Then you can use a library like Mocha to overwrite def post_message during testing. 然后,您可以使用Mocha之类的库在测试过程中覆盖def post_message You can use it to verify that the application is attempting to post a message, and verify the message itself is correct. 您可以使用它来验证应用程序正在尝试发布消息,并验证消息本身是否正确。 It won't actually post the message. 它实际上不会发布该消息。

As I mentioned, you do want to make some tests with real calls to Facebook (though not many). 正如我提到的,您确实想通过对Facebook的真实呼叫进行一些测试(尽管数量不多)。 These could be in a test like you've shown above, which is an integration test, or it could also be a smaller unit test of the Facebook class I suggested above, which would be a better starting point. 这些可以像上面显示的那样在集成测试中进行,也可以是我上面建议的Facebook类的较小单元测试,这将是一个更好的起点。 For this purpose, you'd want to establish a test account on Facebook. 为此,您想在Facebook上建立一个测试帐户。 Then your test should clear all messages in the setup and use Facebook's API to verify that the messages were actually posted. 然后,您的测试应清除设置中的所有消息,并使用Facebook的API验证消息是否已实际发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM