If I want my functional tests to bypass the built in Rails HTTP
authentication, how would I do that?
Would I use something like FlexMock to stub out the :before_filter for
my controller? Could someone please provide an example, because I’m
not really understanding the basics for this scenario.
I’m assuming that the lack of replies suggests that this is the wrong
way to go about. Should I provide some login details in my tests or
what would you suggest?
(which you should replace with Cucumber scenarios)
to bypass the built in Rails HTTP
authentication, how would I do that?
Why do you want to do that? What are you trying to achieve?
Would I use something like FlexMock to stub out the :before_filter for
my controller? Could someone please provide an example, because I’m
not really understanding the basics for this scenario.
It’s hard to understand your use case, and without your ultimate goal,
it’s very difficult to give useful suggestions.
Ah, so the question was unclear. Let me try to rephrase it:
I want to write a test against my controller to make sure that the
actions respond as they should, but since I have HTTP authentication
turned on I get failures like this:
“response to be a <:success>, but was <401>”
So the test runs just fine with authentication turned off, but fails
when turned on. What should I do to get past this and be able to test
the logic of my controller?
Ah, so the question was unclear. Let me try to rephrase it:
I want to write a test against my controller to make sure that the
actions respond as they should, but since I have HTTP authentication
turned on I get failures like this:
“response to be a <:success>, but was <401>”
So the test runs just fine with authentication turned off, but fails
when turned on. What should I do to get past this and be able to test
the logic of my controller?
Have your test code log in at the beginning of your tests. With
controller tests, that probably goes in a before block;
On second thought, in this case, you might want to stub your
before_filter. Or you might not. The rest of my advice stands.
Ah, so the question was unclear. Let me try to rephrase it:
I want to write a test against my controller to make sure that the
actions respond as they should, but since I have HTTP authentication
turned on I get failures like this:
“response to be a <:success>, but was <401>”
So the test runs just fine with authentication turned off, but fails
when turned on. What should I do to get past this and be able to test
the logic of my controller?
With RSpec and FactoryGirl I do something like:
describe “authenticated” do
before(:each) do
login_as(Factory(:user))
end
it “does something as a logged in user” do
…
end
end
/lib/AuthenticatedTestHelper
def login_as(user) @request.session[:user_id] = user ? (user.is_a?(User) ? user.id :
users(user).id) : nil
end
Ah, so the question was unclear. Let me try to rephrase it:
I want to write a test against my controller to make sure that the
actions respond as they should, but since I have HTTP authentication
turned on I get failures like this:
“response to be a <:success>, but was <401>”
So the test runs just fine with authentication turned off, but fails
when turned on. What should I do to get past this and be able to test
the logic of my controller?
With RSpec and FactoryGirl I do something like:
describe “authenticated” do
before(:each) do
login_as(Factory(:user))
end
it “does something as a logged in user” do
…
end
end
That works, to be cure, but why are you using RSpec on your controllers?
That’s a job for Cucumber.
Ah, so the question was unclear. Let me try to rephrase it:
I want to write a test against my controller to make sure that the
actions respond as they should, but since I have HTTP authentication
turned on I get failures like this:
“response to be a <:success>, but was <401>”
So the test runs just fine with authentication turned off, but fails
when turned on. What should I do to get past this and be able to test
the logic of my controller?
Have your test code log in at the beginning of your tests. With
controller tests, that probably goes in a before block; with Cucumber
stories (which I highly recommend using instead), you actually want that
in your scenarios themselves (or your scenario background), in order to
better document and model the workflow.
That works, to be cure, but why are you using RSpec on your controllers?
That’s a job for Cucumber.
Mostly because I pulled this from an older project where I wasn’t yet
using Cucumber. I was just learning RSpec at the time. I might do things
differently today.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.