Thanks for providing such a detailed example guledali! 🙏
Did this run okay for you? It doesn't really work for me sadly, the test still thinks the user is not authenticated, perhaps I'm thinking about building the test the wrong way but have found examples similar to these.
Not sure what I'm missing here, but I don't think it is wrong to think that it's worth testing that a controller action can be accessed and ran when the user is logged in right? 🤔
I got it working now, the mistake I was doing was to add the credentials to a :sessions hash when passing them to :params, which isn't required because of how the form is structured:
# test_helper.rbENV['RAILS_ENV']||='test'require_relative"../config/environment"require"rails/test_help"classActiveSupport::TestCase# Run tests in parallel with specified workersparallelize(workers: :number_of_processors)# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.fixtures:all# Add more helper methods to be used by all tests here...defsign_in_as(user,password)postsessions_url,params: {email: user.email,password: password}endend
An example of a controller test looks like:
test"should show user"dosign_in_as(@user,'password')getuser_url(@user)assert_response:successend
The way I was doing it wrong (adding :session or :sessions):
Thanks for providing such a detailed example guledali! 🙏
Did this run okay for you? It doesn't really work for me sadly, the test still thinks the user is not authenticated, perhaps I'm thinking about building the test the wrong way but have found examples similar to these.
Not sure what I'm missing here, but I don't think it is wrong to think that it's worth testing that a controller action can be accessed and ran when the user is logged in right? 🤔
I got it working now, the mistake I was doing was to add the credentials to a
:sessions
hash when passing them to:params
, which isn't required because of how the form is structured:An example of a controller test looks like:
The way I was doing it wrong (adding
:session
or:sessions
):