Rails 4 integration tests: Routes back to same page - testing

In my routes, I have the following:
match '/:org_id/users/:id/receive_message' => 'users#receive_message', :via => [:get, :post], :org_id => /[a-z0-9.-]+/i, :id => /[a-z0-9.-]+/i
In my route tests I have the following:
assert_routing({method: 'post', path: '/test4.example.com/users/user.test.email/receive_message'}, {controller: "users", action: "receive_message", org_id: "test4.example.com", id: "user.test.email"})
Which works fine. Except when I do an integration test on the action with
post "/123/users/456/receive_message"
I get a 301 redirect to http://example.com/123/users/456/receive_message
I have turned off devise authentication and authenticity_token checking for this action. It never gets to any of the before_* filters in the integration test. The action is working fine in production. Using stock Rails 4 test framework for units, controllers, and integration tests, and FactoryGirl for mocks.
Any ideas?

Related

Is jquery not availble in integration test for rails 3.2 app?

We use a fields_for and jquery to add a partial view on a form in rails 3.2 app. Here is the code:
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render :partial => association.to_s, :locals => {:f => builder, :i_id => 0}
end
link_to_function(name, "add_fields(this, \"#{association}\", \"#{j fields}\")")
end
In applicaton.js:
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
Whenever the 'Add Field' link is clicked, the partial view is rendered and a few input fields are added to the current form. The code works in execution without any problem. However in integration test (capybara & launchy), the click_link('Add Field') did not do anything and failed bringing up the partial. Is jquery not enabled in integration test?
By default Capybara use :rake_test driver on all tests, which is fast but dosen't support JavaScript.
Since this test needs JavaScript, make sure you have turned JS driver on.
describe "some feature", js: true do
# test code
end
This will use default JS driver Selenium.

Whats the best way to integration test a facebook application?

So, I have a facebook App that I am testing. I'd like to test a user's interaction on the app, and I'm having an issue.
The user flow that I'm testing is relatively simple: when a user comes to the in-page facebook app, if they already like the page in which the app is located they will see x-content, if they have not yet liked the page, they will be met with a like gate. My goal is to confirm that my users are able to see certain text once they have like the facebook page.
my test:
describe "facebook" do
it "visit and like page", :vcr do
Capybara.current_driver = :selenium
visit "https://www.facebook.com/pages/Testpage/433439410073990?id=433439410073990&sk=app_454512097948559"
within('#login_form') do
fill_in "email", with: FB_EMAIL
fill_in "pass", with: FB_PASSWORD
click_on "Log In"
end
page.should have_content('Testpage')
within('#timelineHeadlineLikeButton') do
click_on 'like'
end
end
end
my problem is that I can't figure out how to get capybara to like the page.
How are you integration testing you facebook applications?
I don't have the complete example.
test.rb
(You can add it to a initializer and add it if the enviroment is test).
OmniAuth.config.test_mode = true
FACEBOOK_INFO = {
"id"=> "220449",
"email" => "nypee#facebook.com",
}
OmniAuth.config.mock_auth[:facebook] = {
"uid" => '112345',
"provider" => 'facebook',
"user_info" => {"name" => "Nype Aylor", "nickname" => 'Aylor'},
"credentials" => {"token" => 'plataformatec'},
"extra" => {"user_hash" => FACEBOOK_INFO}
}
This simulates the call to omniauth.
So in your test, when you simulate a click to the facebook button,
the response you will get is the one from
OmniAuth.config.mock_auth[:facebook].
I think is the best way to integrate testing with Facebook

Shoulda rspec matchers ensure_inclusion_of

I have a test using shoulda that is failing for reasons I don't understand. Any idea what the fix is for this? I hardcoded the array for testing purposes.
All my other shoulda matcher based tests are working fine.
Validation
validates_inclusion_of :status, :in => ["Active", "Closed"]
Test:
it { should ensure_inclusion_of(:status).in_array(["Active", "Closed"]) }
Failure
Failure/Error: it { should ensure_inclusion_of(:status).in_array(["Active", "Closed"]) }
["Active", "Closed"] doesn't match array in validation
Looking at the source code for that matcher:
https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb#L88
Do you have another validation which prevents nil or blank values for :status?

Rails routes work in console but not server

I'm having trouble with a route that seems to be right in the console but gives me a routing error when I use it in the server. The case is similar to the "edit" and "update" pair. Calling GET 'messages/25/followup' should route to messages#followup, while the same URL with POST should route to messages#followup_send. In my routes.rb file I have
get "messages/:id/followup", :to => "messages#followup"
match "messages/:id/followup", :to => "messages#followup_send", :via => :post
Displaying the routes gives
ruby-1.9.2-p0 :092 > puts fu
GET /messages/:id/followup(.:format) {:controller=>"messages", :action=>"followup"}
POST /messages/:id/followup(.:format) {:controller=>"messages", :action=>"followup_send"}
Testing in the console gives
ruby-1.9.2-p0 :088 > r.recognize_path "/messages/54/followup", :method=>'POST'
=> {:controller=>"messages", :action=>"followup_send", :id=>"54"}
The code in the form is
<form id="edit_message_42" class="edit_message" method="post" action="/messages/42/followup?method=post" accept-charset="UTF-8">
...
<input type="submit" value="Send" name="commit">
However, if I click on the button I get in the log
Started POST "/messages/42/followup?method=post" for 127.0.0.1 at 2012-06-27 13:54:48 +0100
ActionController::RoutingError (No route matches "/messages/42/followup")
The same thing happens if I enter the URL manually (including the "method=post'). I will just get around this for now by using a separate name (e.g. /messages/42/send_followup) rather than relying on the GET-POST distinction, but I would like to understand what is going on here.
Thanks for any ideas.

Rails 3 Flowplayer Gem Usage

I'm attempting to use the flowplayer gem in my Rails 3 app. I've followed the installation instructions
Flowplayer Helper for Rails 3
applications
gem 'flowplayer' in your gem file
rails g flowplayer or rails g flowplayer commercial
add javascript_include_tag 'flowplayer.min.js' to your
application layout
read below
Usage
For JQuery
= flowplayer_for :video, '/flowplayer.swf', 'jquery' do
|player|
- player.playlist [{:url => "video_still.jpg" }, {:url =>
"video_512x288.flv", :autoPlay =>
false, :autoBuffering => true }]
- player.onLoad do
- 'this.unmute();'
For Prototype
= flowplayer_for :video, '/flowplayer.swf', 'prototype' do
|player|
- player.playlist [{:url => "video_still.jpg" }, {:url =>
"video_512x288.flv", :autoPlay =>
false, :autoBuffering => true }]
- player.onLoad do
- 'this.unmute();'
Configs are the same ones here
http://flowplayer.org/documentation/api/index.html
TODO
More documentation
After Step 1 I executed
bundle install
Which was successful.
For Step 2, I executed the first option
rails g flowplayer
Which was successful.
For Step 3, I added
= javascript_include_tag 'flowplayer.min.js'
to the head of my application.html.haml file; which was successful.
For Step 4, I added
%a#video{:style => "display:block;width:512px;height312px;"}
to my home.html.haml file; which was successful and the equivalent of
<a id='video' style='display:block;width:512px;height312px;'>
as per this HTML2HAML converter.
Now I've come to the last portion. I am using jQuery, but I have no idea where to put this last snippet of code.
Any help would be great thanks!
I know this is old but for anyone else - you can put the last bit of code right after the rest of your code. Caveat: There is a bug in the latest flowplayer gem. flowplayer_for only takes two parameters (causing it to always use jQuery) while the example shows three parameters. I've sent the author a pull request to change it.
So (in haml):
- content_for :head do
= javascript_include_tag "flowplayer.min.js"
%a#video{:style => "display:block;width:512px;height312px;"}
= flowplayer_for :video, '/flowplayer.swf'do |player|
- player.playlist [{:url => "video_still.jpg" }, {:url => "video_512x288.flv", :autoPlay => false, :autoBuffering => true }]
- player.onLoad do
- 'this.unmute();'