Can we call a controller method inside rake tasks...? - ruby

Is there any way by which controller methods can be used inside Rake task.

You can always call your endpoint but my guess is that you need some logic that lives inside your controller. The best approach would be to extract that logic into some service and use that service in both the controller and your rake task. You get the bonus of being able to test it more easily.

Related

In WCS, can we invoke a controller command from within a jsp ? If yes, please explain the different methods

From what I've heard, A controller command can be invoked from within a scriptlet. But I am not sure of other methods. Any code level information would be very helpful.
You can also try making AJAX call from the JSP to the controller command.
You really shouldn't be directly executing a controller command from within the scriptlet code of a JSP. You could use AJAX to call a command service. Or you could use a DataBean Command, though they are really meant to be commands that populate the databean not really call controller commands. You may also be in a situation that you need to review your use of a controller command, possibly BOD commands would be a better fit if you are wanting to invoke a service from the JSP during the page generation.
You could create your own mapping of your ControllerCommand to REST.
http://www-01.ibm.com/support/knowledgecenter/SSZLC2_7.0.0/com.ibm.commerce.webservices.doc/tasks/twvrestsamplecmd.htm
Then you use the REST tag to run the ControllerCommand.
http://www-01.ibm.com/support/knowledgecenter/SSZLC2_7.0.0/com.ibm.commerce.component-services.doc/refs/rwvwcfresttag.htm
In the new implementation from IBM in FEP8 this will be done locally if possible and will therefor not add any extra network overhead.
By Using Databean also we can invoke controller commands.
ex : <wcbase:usebean>

Testing a ruby class without the required module files

I'm working on a project in Ruby on Rails. We have a controller action that uses a module within a gem. This gem isn't finished yet and it isn't on the file system.
I was told to mock the module in order to test the controller. Is there a way to test this without the actual gem? Would mocking the 'require' calls work?
We are currently using Mocha for Mocking and Stubbing.
There is a way to mock the imports in python. Maybe there is a similar answer to mocking the requires in ruby.
How to mock an import
Or please let me know what would be the best way to handle this.
Update: The person who told me to mock it, suggested adding a stub file, but that would require adding test code to the controller and I don't want to do that.
Update 2: The controller uses methods declared in the Module.
If you are writing tests to mock the method calls, they would fail. For example,
controller.should_receive(:method_in_non_existent_module).with(args) #=> errors
In a correct Red->Green TDD scenario , this is alright because the next step would be to require the gem/file, include the module and add the method call in the controller and make the test pass. But you'll not be able to make the tests pass since you can't require the file because it doesn't exist yet.
May be the developer who asked you to mock the method meant to do so not in your tests, but in your actual code. For example, he's writing a gem 'dongle-jokes' which has a method that gets the most popular dongle joke from the most recent tech conference. He doesn't want the gem to be a blocker for you to finish the controller and the views so he asks you to use a dummy interface that spits out a dummy response.
Write the tests that fail
Add a file lib/dongle-jokes.rb
Add the following to that file.
module DongleJokes
def joke
"Dongle jokes aren't funny!"
end
end
Require the file and include the module, use the method in the controller.
The test should pass now. You can remove lib/dongle-jokes.rb when you start using the actual gem.
If you're working in Rails you shouldn't need to add a require to the controller anyway, as when you add the gem to your gemfile it will be required automatically on Rails startup.
What your colleague most likely meant was that you should stub the module itself. Are you using rspec for your tests? If so you should be able to use stub_const. Let's say the module is called Payments. You can then write test code like the following:
before do
stub_const("Payments", stub)
Payments.stub(process: "payments successful")
end

In RSpec I want to replace argument to a method call

My app is using OpenURI's open() method, to fetch a web page.
In my spec I want to be able to replace the URL passed in open to the path of a local file.
So when calling open('http://www.google.com') I want to switch that url to /path/to/file.
Is there a build-in way to do this in RSpec?
I do not think rspec provide anything to achieve this. You're better off adding a parameter to the method that invokes open() in your application. This will make the code reusable.
If the application code cannot be modified, you could try to overwrite the open() method to substitute the parameter in your rspec script.
If you are planning to write tests against remote URLs, I'd suggest taking a look at the following:
WebMock: https://github.com/bblimke/webmock
VCR: https://github.com/myronmarston/vcr

MVC TDD : How to write test for Response.Redirect?

I am new to MVC and TDD, so if it is a stupid question please spare me :)
What I am trying to do is , I have created a SignOn controller and I just want to write a Test for that controller. The thing is SignOn controller does Response.Redirect internally, that works fine if it is an proper request, but when I run my test it fails as there is no response object.
so how can I test my SignOn controller, which redirects internally ??
When you want to do a redirect you should use the actionresult: Redirect("url"). In your test you can check if the actionresult is of type RedirectResult and/or if it has the right property values.
Your action method should return a RedirectResult. If you know exactly what the URL should be, you can test the Url property.
I am not a TDD guy but something tells me that HttpContextBase is your friend : http://msdn.microsoft.com/en-us/library/system.web.httpcontextbase.aspx
Here is a good resource on that.
http://weblogs.asp.net/gunnarpeipman/archive/2011/07/16/using-moq-to-mock-asp-net-mvc-httpcontextbase.aspx
Hope this helps.
One of the patterns you can use for testing navigation code inside a controller/presenter is the ApplicationController.
Have a look at the following descriptionPEAA: Application Controller
We have implemented the Aplication Controller in our ASP.NET app.
ApplicationController.Navigate(view: "LoginScreen", argument: userId);
The ApplicationController checks which NavigationWorkflows are registered and delagetes the navigation to the correct class.
In your unit tests you can mock the NavigationWorkflow with something like rhino mocks and pass this to your ApplicationController. Then you can check if the correct navigation methods are called on your workflow.
So instead of calling Response.Redirect, you delegate the navigation responsibility to a separate class that can be replaced with unit testing.

Stubbing a controller method with Sinatra and rspec

So I'm trying to figure out a way of stubbing a controller method in rspec for a Sinatra app. The main reason for this is to test the logical flow of the application and to make sure it calls the necessary functions when certain conditions are met. So, in essence, I want to be able to do something like
controller.should_receive(:fancy_method).and_return("This is a string")
What I'm having difficulty doing is accessing the controller instance within the sinatra app. I am able to override the current functions using a class_eval on the sinatra controller class, but I'd love to assert that these functions actually run.
Anyone have any advice?
Thanks.
Dan, I believe what you really want is to just test the controller actions. From a tester's perspective you shouldn't really care about what it actually called but rather for the output, given a specific input and maybe some other special conditions (that is mocking or stubbing other classes) (1).
You can check the official documentation for Sinatra + Rack::Test or this blog post from devver.net.
(1) : If your controller pages are calling some other classes (models, services, etc) you could mock these instead and put expectations on them. For example :
SomeClass.should_receive(:msg).with(:arg).and_return(:special_value)
Some more info for mocking (with RSpec in this exmaple) can be found on the RSpec documentation pages.

Resources