Skip to content

React Rails Testing #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tmoney100 opened this issue Sep 15, 2015 · 25 comments
Closed

React Rails Testing #353

tmoney100 opened this issue Sep 15, 2015 · 25 comments

Comments

@tmoney100
Copy link

I just updated to 1.3.0, ran rake test and got this error message:

ActionView::Template::Error: undefined method 'react_component' for nil:NilClass app/views/layouts/application.html.erb:30:in '_app_views_layouts_application_html_erb___3811361224884677630_70195218569900'

This only happens when I run my tests and doesn't happen when I run the code in the browser. When I stepped back to 1.2.0 it fixed the issue.

I'm calling react_component in my application.html.erb file.

@rmosolgo
Copy link
Member

sounds like I broke something, hmm

@rmosolgo
Copy link
Member

Ah, is this only in test. I think my implementation depends on Rack middleware doing its thing, let me find a fix for this ...

@rmosolgo
Copy link
Member

could you share the test that's breaking? The tests in this repo pass but I guess there's another kind of test that I missed!

@tmoney100
Copy link
Author

The most basic test that is failing is:

test '.show should succeed' do
    get :show
    assert_response :success
  end

@rmosolgo
Copy link
Member

what kind of test is that? what test framework? is it inside a class which extends a base test class? is it part of a describe block?

@rmosolgo
Copy link
Member

(seems like this middleware is not applied in the test context: https://github.com/reactjs/react-rails/blob/master/lib/react/rails/render_middleware.rb)

@tmoney100
Copy link
Author

We use:

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'

  # Shim to load environment variables from .env into ENV in development. Read more: https://github.com/bkeepers/dotenv
  gem 'dotenv-rails'

  # Letter opener: https://github.com/ryanb/letter_opener
  gem 'letter_opener', :group => :development

  # Headless phantomjs(chrome based browser) for javascrip testing
  gem 'poltergeist', :require => false

  # Ruby Pry, debug ruby https://github.com/deivid-rodriguez/pry-byebug
  gem 'pry-byebug'

  # Static syle checking https://github.com/bbatsov/rubocop
  # version 0.33.0 is causing issues -> when running autocomplete it find multiple infinite loops
  # Untill we solve that problem, we'll go back to the last stable version.
  gem 'rubocop', '0.32.1', :require => false

  # Optional code coverage
  gem 'simplecov', :require => false

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'

  # Javascript testing https://github.com/modeset/teaspoon
  gem 'teaspoon'
  gem 'teaspoon-mocha'
end

group :test do
  # Use webmock for integration testing.  Read more: https://github.com/bblimke/webmock
  gem 'webmock'
  gem 'mocha'
end

@stevestmartin
Copy link
Contributor

@rmosolgo I can confirm this as well when upgrading to react-rails 1.3.0, this is using minitest 5.1 using a controller test extending from ActionController::TestCase

@rmosolgo
Copy link
Member

Ahh awesome, thanks for that info, i can try to reproduce tomorrow!

stevestmartin added a commit to stevestmartin/react-rails that referenced this issue Sep 18, 2015
@stevestmartin
Copy link
Contributor

@rmosolgo I went ahead and sumitted a pull request for a failing testcase that reproduces this problem #355, hope this helps

@rmosolgo
Copy link
Member

YES, that's super helpful, thank you!!

@rmosolgo
Copy link
Member

@stevestmartin could you point your gemfile at this branch and give it a try? #356

for example,

gem 'react-rails', github: 'rmosolgo/react-rails', branch: 'refactor-helper-lifecycle'

@stevestmartin
Copy link
Contributor

@rmosolgo works great, thanks. also using middleware was only temporarily a bad choice. Rails 5 controller tests will work the same as integration tests executing the middleware.

@rmosolgo
Copy link
Member

Thanks for reporting & testing, please try 1.3.1 from rubygems and reopen if you still have this problem!

Good to know about middleware in the future, but I figure we'll be supporting Rails 4 for a few years still 😬

@dapi
Copy link

dapi commented Oct 14, 2015

1.3.2 still broken for rspec

@javierjulio
Copy link

I can confirm this is still failing on 1.3.2. I have a cache warming rake task that renders a view page which uses react_component in there. On version 1.2.0 it wasn't an issue but after upgrading I receive the same error: ActionView::Template::Error: undefined method 'react_component' for nil:NilClass

@rmosolgo
Copy link
Member

Sorry, I'm on the road this week so I haven't had a chance to properly address this issue. There's a PR to fix Rspec here: #367

Perhaps something there will be useful in the meantime!

@nacengineer
Copy link

FWIW this also also breaks using react_component within the cells gem (in development) the 3.0 branch not current 4.0 branch. Perhaps this may help create a fix in isolation. It's not broken in 1.2.0. Still broken in 1.3.2

Also as a side note, perhaps its just me but if it were my gem and I knew it was broken (even if for a smaller subset) and is present in rubygems, I would advise yanking those versions.

@rmosolgo
Copy link
Member

@nacengineer Thanks for the heads up about the "cells" gem, I'll have to take a look at that!

I hear your point about putting imperfect code on Rubygems. But I don't want the perfect to be the enemy of the good! 1.3.0 introduced the per-request view helper implementation, which I want to support.

The broken use case here is to use a Rails view helper without a Rails controller. I think I can fix that case, but I'm just without a home base at the moment (moving from CA to VA 😬)!

@nacengineer
Copy link

@rmosolgo congrats on getting back east of the Mississippi, live is better on this side.

FWIW all I have to do to trigger it is render a cell and use the the react_component helper in its view. Nothing special.

@rmosolgo rmosolgo mentioned this issue Oct 17, 2015
@rmosolgo
Copy link
Member

I've suggested a fix here: #375

This way, the view helper will work even if you're not in the context of a controller action. Could you give it a try and let me know if that works for you?

Sorry I overlooked this when adding that feature!!

@javierjulio
Copy link

@rmosolgo I just chimed in to mention it was still failing but when I described how I was using it I realized I could workaround it. But also probably not at all how you intended someone to use the helper so I didn't expect a fix for an odd case. Just wanted to say thanks so much for taking the time! Really appreciate it.

@rmosolgo
Copy link
Member

Shipped in 1.3.3, please let me know if you find a case where it's still broken!

@javierjulio
Copy link

@rmosolgo just gave it a try, its working for my use case. Thanks!! 👍🏻🎉

@nacengineer
Copy link

@rmosolgo Can confirm it fixes the issue in cells gem. Thanks! I'm always impressed with the responsiveness of react-rails gem's devs. Thanks

I would still put a small plug in to yank previous 1.3.x gem as they're technically broken. Just from a technical support POV I don't think you want to rely on people coming across this thread if they run into the issue. IMO its not a better v. good issue. Its broken v. working. But its of course your call.

Best of luck in VA! Thanks for the timely response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants