I have a helper method called current_user in my Application Controller (used with Authlogic).
Spec for views using that helper fail (but the view is working when i use the browser)
ActionView::Template::Error: undefined local variable or method 'current_user' for #<#<Class:0x0000000229b060>:0x00000002004248>
I use rspec 2.6.0.
Anyone had the same problem? Please advice. Thanks
You can stub out the method on view.
view.stub(:current_user).and_return(user)
This will also work in helper specs.
Controller-defined helper methods are not included in the helper object.
http://relishapp.com/rspec/rspec-rails/dir/helper-specs/helper-spec
Check out this one: http://apidock.com/rails/ActionController/Helpers/ClassMethods/helper_method.
You can define helper inside the ApplicationController, and use it across the controllers AND views.
check right documentation for specs view
https://www.relishapp.com/rspec/rspec-rails/v/2-14/docs/view-specs/view-spec#passing-view-spec-that-stubs-a-helper-method
Related
i have a module called 'elite' which render articles and another module called 'inet'.
Inet->controllerX need to get the HTML only from an article(elite->article->actionView).
so my question is:
should i call elite->article->actionView from another module or use components and inheritance and how?
Thank you!
You can easily render a view in any module, just need view path.
return $this->render('#app/modules/elite/views/name_controller/name_view');
You must replace the name of the controller and the name of the view.
I'm looking for a way to dynamically provide at runtime view models for any views. Question is if there is hook in the ViewEngine or a view model loader / factory that I can use to do this.
I'm looking into bridging into WebAssembly and allowing view models to be written in other languages and I want to create automatic interop view models on the Javascript side for Aurelia to just work.
This is totally doable, and many have successfully delivered their ultra-dynamic application using Aurelia, as pretty much everything in Aurelia is dynamic.
For example:
dynamic, instance based inline view: https://codesandbox.io/s/r0no20706p (Based on this discourse topic https://discourse.aurelia.io/t/template-literals-in-class-instance/1829/6)
dynamic, remote view strategy: https://codesandbox.io/s/8ljmrq3vql (Based on a github question)
dynamic, route based view model: https://codesandbox.io/s/ywyj60p9qj (based on this discourse https://discourse.aurelia.io/t/ideas-on-data-driven-ui-composition-vs-previous-angularjs-approach/2269/3
Basically it boils down to is to use .compose method from CompositionEngine:
compositionEngine.compose({
viewModel:
string // as module path
| Function // as constructor
| object // as instance
})
And make sure the view is resolvable from the view model.
Given an example based on old MVC5:
Views/Shared/Index.cshtml - a view for a SPA app. It contains some markup and a reference to a layout-page:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
In _Layout.cshtml there're number of includes which are being used via RenderPage helper:
#RenderPage("~/Views/Shared/_ImportCssInline.cshtml")
#RenderPage("~/Views/Shared/_ImportCssLinks.cshtml")
Now in AspNet5 #RenderPage helper isn't available.
It was the method of WebViewPage/WebPageBase/WebPageRenderingBase. Now they were replaced by RazorPage. But there's no RenderPage method in it.
What should be used instead?
p.s. issue
I've always had success using #Html.Partial("~/Views/Shared/_ImportCssInline.cshtml") rather than #RenderPage - I hope there's not usage differences for you. There are also async versions of these imports now, too.
Since the Html property is now injectable as the interface IHtmlHelper, I assume the direct methods were removed in the improvements for the testability of the views.
I am new in Yii. And some things in this framework i am understand well. But i am can't understand how work Yii::app() and where i can find Yii::app()->user->checkAccess method?
Should you are explain me it. Thanks!
Yii::app()->user is the user component, which is defined in your config file (usually /protected/config/main.php). In the components array you will find a 'user' component. The default class for this is CWebUser, so probably 'checkAccess' is defined in CWebUser (did not check this though).
You can write your own class extending CWebUser if you want to override this property (it's not a method).
See your /protected/config/main.php file, and then you may find authManager section in components. In my case, I set the class of authManager to CDbAuthManager. In that class, checkAccess method is defined.
What's the proper way to disable ActionController::TestCase to actually render the views?
Alternatively, what'd be the way to render the view without the layout in the tests?
Using rr, I've tried
stub(#controller).render { "" }
but this broke the assert_template assertions.
Thanks!
I had the same problem of disabling just layout, while still rendering the main view. With rspec mocks this works for me:
#controller.stub(:layout).and_return(false)
I've never used rr, but I would imagine the syntax might be as follows:
stub(#controller).layout { false }
I wanted to do this in Rails 6. With the the mocha gem:
ActionController::Metal.any_instance.stubs(:process).returns
For other versions of Rails, the easiest way to determine the answer is to add puts caller to your action and scan the backtrace. Find something that looks easy to stub and try it.
It is by default disabled.
If you want to enable you can:
1) call this in spec_helper:
config.render_views
2) put this statement inside your controller test class:
render_views
If you want to disable it, just add a false parameter like:
render_views false
Reference: https://www.relishapp.com/rspec/rspec-rails/v/2-5/docs/controller-specs/render-views