0
votes

I am trying to create test to admin panel. But it fails while the program try to log_in.

Failures:

1) Visit products login works correctly Failure/Error: expect(page).to have_content("Logged in successfully")

expected to find text "Logged in successfully" in "Login\nAll departments\nHome\nCart: (Empty)\n \nInvalid email or password.\nLogin as Existing Customer\nRemember me\nor Create a new account | Forgot Password?" # ./spec/features/home_spec.rb:14:in `block (2 levels) in '

The password, and the email are correct for admin. I found solutions in other posts, like adding configuration to capybara, but it still fails.

spec_helper

require 'capybara/rspec'
require 'rails_helper'
require 'spree/testing_support/controller_requests'
require 'capybara/rails'

Capybara.app_host = "http://localhost:3000"
Capybara.server_host = "localhost"
Capybara.server_port = "3000"

_spec.rb

require "spec_helper"

RSpec.describe 'Visit products' do
    it 'login works correctly' do
        visit spree.admin_path
        fill_in "spree_user[email]", with: "[email protected]"
    fill_in "spree_user[password]", with: "password"
    click_button Spree.t(:login)
        expect(page).to have_content("Logged in successfully")
    end
end
1
Since the page is showing Invalid email or password either the email or password aren't correct, or the user for the test isn't being created correctly. How are you creating the user for the test, and what version of Rails are you using?Thomas Walpole
Also, do you really need have a need for setting server_host, server_port, and app_host ?? For most people leaving those as the defaults is a better config (doesn't prevent you running your dev instance at the same time, etc)Thomas Walpole
As I said, I was trying this configuration because I found it somewhere. It is not changing anything. I think my problem is caused by that I have to use older version of spree, and get error when I try "bundle exec rake test_app" / Don't know how to build task 'test_app'. I think that it is nessecary to pass it to test admin panel in spree due to spree_auth_devise. I am using Rails 5.1.4, spree 3.4.0.petrov
You didn't answer where you're creating the user -- When running in a test mode the app doesn't use your development database, it has its own database and you need to create all the objects (like users) you expect to exist for the test. In your test you don't show creating any users so none will exist. You need to use fixtures or something like factory_bot to create users before your test run.Thomas Walpole
Ok -- added as an answer so you can mark the question as answeredThomas Walpole

1 Answers

1
votes

Since the page is showing "Invalid email or password" either the email or password aren't correct, or the user for the test isn't being created correctly. Since you don't show the creation of any test users in your test it's most likely there aren't any. When running in test mode the app doesn't use your development database, it has its own database and you need to create all the objects (like users) you expect to exist for the test. You can do this by using fixtures or something like factory_bot to create users before each test.

Additionally, there should be no need to set server_host,server_port, or app_host in your situation.