solved: test has dependencies on "live" state of personal_accounts & register_with_account settings, needs these 3 stubs

Some JSP tests OTB do properly stub for some Jumpstart.config settings, for example users_controller_test.rb line 44:

Jumpstart.config.stub(:register_with_account?, true) do
     does tests
end

However, in that same file, a couple of stubs are missing, so the tests use the "live" setting vales, breaking the tests if you change the settings in development.

Specifically:

if set: personal_accounts: false on Development (via Jumpstart config)

Tests start failing with:

Error:
UsersControllerTest#test_turbo_native_registration:
NoMethodError: undefined method `name' for nil:NilClass
    test/controllers/api/v1/users_controller_test.rb:33:in `block in <class:UsersControllerTest>'


if set: register_with_account: true on Development (via Jumpstart config)

Tests start failing with:

Failure:
UsersControllerTest#test_returns_user_and_api_token_on_success [/Users/buzz/ror/jspvirgin/test/controllers/api/v1/users_controller_test.rb:14]:
Expected response to be a <2XX: success>, but was a <422: Unprocessable Entity>
Response body: {"errors":{"owned_accounts.name":["can't be blank"]},"error":"Owned accounts name can't be blank"}

Failure:
UsersControllerTest#test_turbo_native_registration [/Users/buzz/ror/jspvirgin/test/controllers/api/v1/users_controller_test.rb:25]:
Expected response to be a <2XX: success>, but was a <422: Unprocessable Entity>
Response body: {"errors":{"owned_accounts.name":["can't be blank"]},"error":"Owned accounts name can't be blank"}

Solution:

Modifying users_controller_test.rb as shown below makes the tests independent of the "live" values of those two Jumpstart config settings:

  test "returns user and api token on success" do
    email = "api-user@example.com"

    Jumpstart.config.stub(:register_with_account?, false) do
      assert_difference "User.count" do
        post api_v1_users_url, params: {user: {email: email, name: "API User", password: "password", password_confirmation: "password", terms_of_service: "1"}}
        assert_response :success
      end
    end

    assert response.parsed_body["user"]
    assert_equal email, response.parsed_body.dig("user", "email")
    assert_not_nil response.parsed_body.dig("user", "api_tokens").first["token"]
  end

  test "turbo native registration" do
      Jumpstart.config.stub(:personal_accounts, true) do
        Jumpstart.config.stub(:register_with_account?, false) do
          assert_difference "User.count" do
            post api_v1_users_url, params: {user: {email: "api-user@example.com", name: "API User", password: "password", password_confirmation: "password", terms_of_service: "1"}}, headers: {HTTP_USER_AGENT: "Turbo Native iOS"}
            assert_response :success
          end
        end
      end

      user = User.last

      # Account name should match user's name
      assert_equal "API User", user.personal_account.name

      # Set Devise cookies for Turbo Native apps
      assert_not_nil session["warden.user.user.key"]

      # Returns a location and API token
      assert_not_nil response.parsed_body["location"]
      assert_equal user.api_tokens.find_by(name: ApiToken::APP_NAME).token, response.parsed_body["token"]
  end
Notifications
You’re not receiving notifications from this thread.
Subscribe
© 2022 GoRails, LLC