jonleighton / focused_controller Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
This seems like a bad abstraction in hindsight and obscure the intent of the tests.
Given the following routes
focused_controller_routes do
resources :posts do
get :something, on: :member
end
end
and the following functionnal test
require 'spec_helper'
require 'focused_controller/rspec_functional_helper'
describe PostsController do
include FocusedController::RSpecFunctionalHelper
describe PostsController::Something do
it "should not raise an error" do
get
end
end
end
I got the this error
1) PostsController PostsController::Something should not raise an error
Failure/Error: get
ActionController::RoutingError:
No route matches {:controller=>"posts_controller/something", :action=>"call"}
This action works perfectly when accessed via a browser and routes seem to be correctly defined when running rake routes
Whenever I assert the rendering of a template, I need to explicitly call render
inside the call
method for the specs to pass.
Going down the stack a bit, it seems that the code breaks at test_helper.rb#L126, more specifically, controller._render_options
returns nil without an explicit render
.
I searched to see if this was expected test/spec behavior, but I couldn't find others having the same problem, so I suspect it to be related to FocusedController.
Instance variables set in call are not available in template.
module FooController
class Bar < Action
def call
@foo = 'foofoo'
end
end
end
bar.html.erb
<%= @foo.inspect %>
Outputs 'nil'.
Hey Jon, was just wondering if you had rails4.1 support on your roadmap. I tried to add 4.1 support myself, but found it not to be a trivial upgrade. Thanks!
I'm wondering if you'd considered using :call instead of :run for the method name that does the rendering?
It happens because the req
method simply calls the controller's run
method: https://github.com/jonleighton/focused_controller/blob/master/lib/focused_controller/test_helper.rb#L117
The license is MIT. Need to make this explicit.
Hi,
I'm just curious if you think that F.C would support Rabl as it is?
https://github.com/nesquena/rabl
Thanks!
Focused_controller has no Mailing list to speak about it and debate like do by wycats and jonleighton on twitter.
Please create an open mailing list to speak about concept behind focused_controller.
Thanks
Took me a while to figure out that I had to put the focused_controller_routes do
within the outer draw do
and not replace it.
Please provide a small but complete example of a routes.rb
file using the new syntax and extra sugar, such as draw
, perhaps with an example of a file structure, with the external routes file. Thanks!
See the below specification:
context 'For valid authentication' do
Given { subject.params = { authentication: { email: '[email protected]' } } }
Given { subject.authentication.stub(:legitimate?).and_return(true) }
Given { Authentication.any_instance.stub_chain(:user, :id).and_return(1) }
When { subject.call }
context 'deletes signup session' do
Then { subject.session[:signup].should be_nil }
end
context 'sets authentication cookie' do
Then { subject.send(:cookies)[:authentication].should_not be_nil }
end
context 'sets authentication cookie' do
Then { subject.cookies[:authentication].should_not be_nil }
end
end
The first two pass, the last one fails. I was under the impression that I should be able to access cookie data in my FocusedController
specs (I have included FocusedController::RSpecHelper
) but only session
seems to be available here?
Here is the error stacktrace:
1) SessionsController SessionsController::Create For valid authentication sets authentication cookie
Failure/Error: Then { subject.cookies[:authentication].should_not be_nil }
NoMethodError:
private method `cookies' called for #<SessionsController::Create:0x007fc818852f90>
# ./spec/unit/controllers/sessions_controller_spec.rb:47:in `block (5 levels) in <top (required)>'
# ./spec/unit/controllers/sessions_controller_spec.rb:47:in `block in Then'
After debugging by forking from master and using my own local repo copy, it turns out that expose
as described in the README is not supported by the latest published gem.
Warning to all: use only the master directly for now.
The travis-ci tests haven't passed in a while. I attempted to run the test locally, but they wouldn't run cleanly (or finish for that matter).
In general, do you plan to keep this project under active development? (I'd be willing to help maintain in either case)
@avdi has proposed a DSL syntax: https://gist.github.com/3757036
I initially resisted adding a DSL to FC because:
However, I am coming around to the idea:
But let's go one step at a time and get something working, try it out in real applications, then iterate.
I like Avdi's syntax, but have some comments:
BenefitsController = FocusedController::Controller.new do
is a bit "out there" I think, and using techniques that people might find confusing.create
inherit new
and update
inherit edit
. So here's my stab at it:FocusedController.define :posts do
common do
before_filter :authorize
end
action :index do
expose(:posts) { Post.all }
end
action :new do
expose(:post) { Post.new }
end
action :create, extends: :new do
def call
post.update_attributes params[:post]
redirect_to posts_path
end
end
shared :find do
expose(:post) { Post.find params[:id] }
end
action :show, extends: :find
action :edit, extends: :find
action :update, extends: :edit do
def call
# ...
end
end
action :destroy, extends: :find do
def call
post.destroy
redirect_to posts_path
end
end
end
I am not sure about the shared
thing. An alternative is:
group do
expose(:post) { Post.find params[:id] }
action :show
action :edit
action :update, extends: :edit do
def call
# ...
end
end
action :destroy do
def call
post.destroy
redirect_to posts_path
end
end
end
Not sure about that either.
Alternatively edit
, update
and destroy
could technically just extend show
, but that feels a bit wrong conceptually although it seems fine pragmatically and is less verbose.
Hey Jon,
Any plans to make this rails4 compatible? I added an appraisal and the tests don't run.
I'm happy to take a look as well, but I just got started with rails4, so it may take me some time to get up-to-speed.
The focused_controller_routes
helper appears to not support the as: 'named_route'
construct:
focused_controller_routes do
get '/login' => 'sessions#new', as: 'login'
get '/logout' => 'sessions#destroy', as: 'logout'
resources :sessions
end
rake routes
:
login GET /login(.:format) sessions#new
logout GET /logout(.:format) sessions#destroy
sessions GET /sessions(.:format) SessionsController::Index
POST /sessions(.:format) SessionsController::Create
new_session GET /sessions/new(.:format) SessionsController::New
edit_session GET /sessions/:id/edit(.:format) SessionsController::Edit
session GET /sessions/:id(.:format) SessionsController::Show
PUT /sessions/:id(.:format) SessionsController::Update
DELETE /sessions/:id(.:format) SessionsController::Destroy
@jonleighton provided a workaround on the mailing list, which works well:
focused_controller_routes do
get '/login' => 'SessionsController::New', as: 'login'
get '/logout' => 'SessionsController::Destroy', as: 'logout'
resources :sessions
end
login GET /login(.:format) SessionsController::New
logout GET /logout(.:format) SessionsController::Destroy
sessions GET /sessions(.:format) SessionsController::Index
POST /sessions(.:format) SessionsController::Create
new_session GET /sessions/new(.:format) SessionsController::New
edit_session GET /sessions/:id/edit(.:format) SessionsController::Edit
session GET /sessions/:id(.:format) SessionsController::Show
PUT /sessions/:id(.:format) SessionsController::Update
DELETE /sessions/:id(.:format) SessionsController::Destroy
If I can make some time, I'll take a crack at a patch. I took a cursory look at it, but I don't yet understand how it's all working. Any direction someone could provide would be most welcomed.
Need to properly consider impact on action/fragment caching and make sure they work properly.
If I check the params fill in my controller. the controller is not fill like do in Rails way. If I do a params[:controller]
on my controller, it's return nil.
I think it's better to return the controller name.
Hello,
are you planning on allowing to use focused controller with Rails 4.2?
Look into whether it's possible/practical to remove e.g. assert_redirected_to foo
in favour of assert_equal foo, response.redirected_url
.
On my project I have a route :
focused_controller_routes do
resources :urls
end
I want test it on my rspec routing like :
describe "url resource" do
it 'should see /urls' do
get('/urls').should route_to(
:controller => 'urls::index',
:action => 'run'
)
end
end
But it's failed :
1) url resource should see /urls
Failure/Error: get('/urls').should route_to(
No route matches "/urls"
# ./spec/routing_spec.rb:5:in `block (2 levels) in <top (required)>'
Lets say you have these routes:
Blog::Application.routes.draw do
focused_controller_routes do
resources :sessions, only: [:new, :create, :destroy]
resources :accounts do
resources :posts
resources :ideas
end
root to: "PagesController::Splash"
end
end
And now you want to link to the idea form:
"Hello, make a new #{link_to "idea", new_account_idea_path}"
Now this will error out, saying:
ActionController::RoutingError (No route matches {:action=>"run"}):
app/helpers/accounts_helper.rb:19:in `no_ideas_text'
app/views/accounts/_no_ideas.html.slim:1:in
`_app_views_accounts__no_ideas_html_slim___4495721604514828093_70275587376260'
app/views/accounts/show.html.slim:7:in
`_app_views_accounts_show_html_slim__1164712277643331339_70275587639940'
The error page is Rail's "bad route error", so it wont even show the last 4 lines of that error just the routing erro text.
The "problem" here is that I forgot to provide an account_id:
new_account_idea_path(account)
But it doesn't recognize that.
It should be possible to override the described class via self.controller_class = Foo
.
Hi,
I have the following action
module ConnectorsController
...
class Create < Action
expose(:connector)
def call
@connector = Connector.new(params[:connector])
@connector.save
render :json => { :status => 'ok' }
end
end
If i tried to call it with manually, for example with curl, everything works perfectlly. But when I tried to test it with RSpec
Failure/Error: subject.call
RuntimeError:
ActionController::RackDelegation#content_type delegated to @_response.content_type, but @_response is nil
Here's the test I used, pretty basic
describe ConnectorsController::Create do
it "should render JSON" do
subject.params = { :connector => {} }
subject.call
puts response.body
end
end
I also tried to replace the JSON by render :action => 'show'
but without success.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.