Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you run specs on ForestLiana collections and controllers ? #629

Open
jwoodrow opened this issue Aug 10, 2023 · 1 comment
Open

How do you run specs on ForestLiana collections and controllers ? #629

jwoodrow opened this issue Aug 10, 2023 · 1 comment

Comments

@jwoodrow
Copy link

I've been going crazy about this for about a day now and I'm at my wits' end here.

Since my Collections and Controllers have some custom fields and actions I wanted to be thorough and test them out in RSpec and for the life of me I cannot figure out how the gem does this at any point.

I've looked into the ResourceGetter, SearchQueryBuilder, ApplicationController and its serialize_model method and then into forestadmin-jsonapi-serializers and nothing seems to do the trick, I am always stuck with just my normal attributes and nil for my smart fields.

I feel like it should be "easy" and standard for a gem to be testable especially when the usage can be customized which in turn would lead to untested code.

So my question is "simple", how does one use RSpec (or any other testing library for that matter) and actually test the forest smart fields/relations/actions ?

@nicolasalexandre9
Copy link
Member

Hi @jwoodrow,

Unfortaunalbly we don’t have any helpers or functions to test the smart features.
We will soon publish a new version of the agent wich will contain these helpers.
For the moment to testing smart action you can call the endpoint generated and for the smart fields there is an example below.

require 'rails_helper'

RSpec.describe ForestLiana::ResourcesController, type: :request do
  before do
    ForestLiana.env_secret = 'env_secret_test'
    ForestLiana.auth_secret = 'auth_secret_test'
    ForestLiana::Bootstrapper.new
  end

  it 'should return the user serialized with the smart field "full_name"' do
    user = create(:user)

    controller = ForestLiana::ApplicationController.new
    options = {
      :field => {
        'User' => 'full_name'
      },
      :skip_collection_check => false
    }

    json = controller.serialize_model(user, options)

    expect(json['data']['attributes']['full_name']).to eq "#{user.first_name} #{user.last_name}"
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants