Writing specs for put to a custom path/model

I’m working on a plugin that adds a model. I am trying to write specs that test that the model (and controller?) are behaving as expected.

This code in an ansible library, is successfully PUTting to this path and updating a field in the model/record:

    headers = {
        'Api-Key': discourse_api_key,
        'Api-Username': discourse_api_username,

    payload = {
        "server[request_status]": status
    url = "%spfaffmanager/servers/%s" % (discourse_url, server_id)

    # raise Exception(url) 
    result = requests.put(url, data=payload, headers=headers)

This is the spec that I am trying to write that tests updates to that path. I’m confused because I have a zillion puts in the controller and none of them are printing, though puts in the model (e.g., in a function called by before_save ) are showing up in the stuff I see in the rake autospec.

My best guess is that I’m screwing up the

# frozen_string_literal: true
require 'rails_helper'

describe Pfaffmanager::ServersController do
  fab!(:user) { Fabricate(:user) }
  fab!(:admin) { Fabricate(:admin) }
  fab!(:another_user) { Fabricate(:user) }
  fab!(:trust_level_2) { Fabricate(:user, trust_level: TrustLevel[2]) }
  before do

  it 'can update status' do
    request_status = 'new status'
    s=Pfaffmanager::Server.createServerFromParams(user_id: user.id,
        hostname: 'bogus.invalid' , request_status: 'not updated')
    puts "can update status created server id: #{s.id}"
    params = {server: {request_status: request_status}}
    expect {
        put "/pfaffmanager/servers/#{s.id}", params: params
    }.to change { s.request_status }
    expect(response.status).to eq(200)
    expect(s.request_status).to eq('new status')

I spent pretty much all day on this yesterday and would appreciate any hints on solutions or techniques for debugging.

1 Like

If s=Pfaffmanager::Server.createServerFromParams(user_id: user.id, hostname: 'bogus.invalid' , request_status: 'not updated') is the model you’ve created, you might need to fabricate it for the request test to work properly, similar to how the users are fabricated previously in your example.

1 Like

Oh. So fabricating it rather than doing an actual create might be my problem? In the other specs, I, for example, use the above code to create a Server and then do a get to get the list of servers and confirm that a server with the proper hostname exists and that the correct number of servers gets returned.

It probably makes sense to figure out how to fabricate one the “right” way, though.

Thanks very much for your help. I’ll try that next!

1 Like

I figured out my problem (or one of them, anyway). If you have errors in your model.rb file like something that tries to do something like variable_that_is_nil['xxx'], then the model fails but you don’t see any errors in the spec logs like you do in the rails log if you were “testing” with a browser. So it can seem like the model or controller isn’t getting called when it really is.

But I did actually write an actual Fabricator, so thanks for the push, @justin!

1 Like