-1

I'm building a activity stream feature in my app, all activities are created via controller methods.

When the controller method is successful the activity is recorder to activity table. Since my controller action is invoked via ajax, I tried posting directly from my rspec test.

I tried this :

Net::HTTP.post_form(URI.parse("http://localhost:5000#{inquiry_path}"), { :id=> @user.id, :type => 'Inquiry' })

This is what I see gets posted in my firebug, when making ajax call:

id:55
type:Inquiry
url:/user_inquiry

So I'm asserting whether the activity count has changed :

it "activity count should be changed" do
  PublicActivity::Activity.count.should == 1
end

After I'm done with testing that count has changed, I wanted to see the user home page actually displaying the new notification.

But this test fails :

Failure/Error: PublicActivity::Activity.count.should == 1
       expected: 1
            got: 0 (using ==)

Just in case I put the puts statement in my controller, to check if the call actually reaches the controller :

puts "Hereeee"

I don't see the message getting printed so I guess the request never reaches the controller, why is that?

Am I doing something wrong here, how can I test this?

Gandalf StormCrow
  • 25,788
  • 70
  • 174
  • 263
  • 1
    First of all you should probably do a controller or feature spec (depending on what you are testing) and use the rspec methods for requests e.g `get`,`post` which handle all the server runtime as well. Other than that a common cause of problem could be that you are not logged in and you have restricted unauthorized user access to that action. You should first login and then add the user session in the request. – Nikos Feb 17 '14 at 16:59
  • I logged in already but that was not the issue, and I have the controller/feature specs separately. thanks – Gandalf StormCrow Feb 17 '14 at 18:52

1 Answers1

-1

My test idea was ok except the part where the request didn't arrive to the controller, here is how I solved it (before and it):

page.driver.post("#{inquiry_path}?id=" + @user.id)

it "post request should return the right response code" do
    page.driver.status_code.should eql 200
  end

Now my requests are arriving to the controller.

Gandalf StormCrow
  • 25,788
  • 70
  • 174
  • 263
  • @phoet found it here btw. http://stackoverflow.com/questions/4084212/how-do-you-post-to-a-url-in-capybara , not sure what you mean by worst kind of test, you only saw one portion of my test – Gandalf StormCrow Feb 17 '14 at 18:57