0

Full error messages:

1) RelationshipsController creating a relationship with Ajax should increment the     Relationship count
Failure/Error: xhr :post, :create, relationship: { followed_id: other_user.id }
ArgumentError: bad argument (expected URI object or URI string)
# ./spec/requests/relationships_controller_spec.rb:14:in `block (4 levels) in <top (required)>'
# ./spec/requests/relationships_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

2) RelationshipsController creating a relationship with Ajax should respond with success
Failure/Error: xhr :post, :create, relationship: { followed_id: other_user.id }
ArgumentError: bad argument (expected URI object or URI string)
# ./spec/requests/relationships_controller_spec.rb:19:in `block (3 levels) in <top (required)>'

3) RelationshipsController destroying a relationship with Ajax should decrement the Relationship count
Failure/Error: xhr :delete, :destroy, id: relationship.id
ArgumentError: bad argument (expected URI object or URI string)
# ./spec/requests/relationships_controller_spec.rb:31:in `block (4 levels) in <top (required)>'
# ./spec/requests/relationships_controller_spec.rb:30:in `block (3 levels) in <top (required)>'

4) RelationshipsController destroying a relationship with Ajax should respond with success
Failure/Error: xhr :delete, :destroy, id: relationship.id
ArgumentError: bad argument (expected URI object or URI string)
# ./spec/requests/relationships_controller_spec.rb:36:in `block (3 levels) in <top (required)>'

My github for source of code

In browser AJAX works fine but tests is red. :(

I'm pretty new to programming, Rails and Stackowerflow. Please help me to solve this problem. :3

0

2 Answers 2

2

I had exactly the same issue following the same Ajax section of Chapter 11 in the RailsTutorial(with Rails 3.2). I think Michael de Silva was right in that the xhr were unfortunately mixed with the RSpec here.

Anyways, I decided that for the sake of completeness (I'm almost done with the tutorial) - I was going to get xhr to somehow work here. I think Mike Hartl's Listing 11.37 should have used the ActionDispatcher::Integration::RequestHelpers method xhr, as in http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-xhr

rather than the ActionController::TestCase::Behavior method xhr, as in http://api.rubyonrails.org/classes/ActionController/TestCase/Behavior.html#method-i-xhr

So I replaced the reference to actions :create and :destroy with their named routes and the test examples in Listing 11.37 turned green. The original

expect do
  xhr :post, :create, relationship: { followed_id: other_user.id }
end.to change(Relationship, :count).by(1)

becomes,

expect do
  xhr :post, relationships_path, relationship: { followed_id: other_user.id }
end.to change(Relationship, :count).by(1)

and, the original

expect do
  xhr :delete, :destroy, id: relationship.id
end.to change(Relationship, :count).by(-1)

becomes,

expect do
  xhr :delete, relationship_path(relationship.id), id: relationship.id
end.to change(Relationship, :count).by(-1)
0
0

See if this helps: RSpec test destroy method (Rails Tutorial 3.2 Ch. 9, Ex. 10)

I'd also read further down in the link above; you should make maximum use of Capybara instead of mixing rspec with methods provided by Rails for Action*::TestCase. Do this instead

you can use capybara's page.execute_script (but you have to enable javascript for this example :js => true)

2
  • Sorry, but i want use method exactly as in Rails Tutorial. For example "xhr :post, :create, relationship: { followed_id: other_user.id }". And want to know why test are red wis thes line of code. May 12, 2013 at 15:33
  • That's exactly my point, you really shouldn't be using the xhr method if you can help it. Go refer it up @ api.rubyonrails.org. It's listed under ActionController::TestCase::Behavior...but you're using RSpec! If you were say using Test::Unit or MiniTest, then it's a different story. Hence the suggestion to use Capybara to make the AJAX request. Hartl shouldn't be encouraging this! May 13, 2013 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.