ruby on rails - Testing an API resource with RSpec -
i've done bit of googling on topic, , i'm still confused.
i'm building custom page zendesk api ruby client, , i'm @ stage when need test creation of zendeskapi::ticket
resource. following code in spec/features directory. fills out form valid values , submits form #create
action. standard, simple stuff.
require 'spec_helper' feature 'ticket creation' scenario 'user creates new ticket' visit root_path fill_in 'name', with: 'billybob joe' fill_in 'email', with: 'joe@test.com' fill_in 'subject', with: 'aw, goshdarnit!' fill_in 'issue', with: 'my computer spontaneously blew up!' click_button 'create ticket' expect(page).to have_content('ticket details') end end
and here relevant part of tickets controller. ticket_valid?
method supplies minimal validations options hash , client
instance of zendeskapi::client
.
def create options = { subject: params[:subject], comment: { value: params[:issue] }, requester: params[:email], priority: 'normal' } if ticket_valid? options flash[:success] = 'ticket created.' @ticket = zendeskapi::ticket.create(client, options) redirect_to ticket_path(@ticket.id) else flash[:error] = 'something went wrong. try again.' redirect_to root_url end end
problem is, whenever run tests, actual ticket created in zendesk backend i'll have delete manually later when want test successful form submission without creating ticket.
so question is, how can test ticket creation form without creating actual ticket in zendesk backend whenever run tests?
the articles , blogs i've been reading result of googling vaguely refers using racktest, while others suggest not using capybara @ sort of thing, leaves me more confused. i'm still relatively new rspec , newer dealing building rails apps api, clear explanation great.
thanks in advance!! you're awesome.
if don't want call zendesk, you'll have create "test double" instead of actual call. test double capability comes rspec described minimally @ https://github.com/rspec/rspec-mocks, covered more comprehensively in blogs , books.
the answer posted simultaneously discusses creating separate class, still seems involve creating zendesk ticket. don't need separate class , don't need create zendeskapi objects @ all. instead, stub zendeskapi::ticket#create
return test double in turn need serve whatever zendesk ticket methods rest of test needs, @ least includes id
.
the use of capybara secondary issue , refers how drive test. note test requires rendering ticket page , checking content of page. if want test "just" controller, can/should test makes proper calls (e.g. zendeskapi::ticket
) , redirects appropriate page. further, if that, have less simulate in test double.
Comments
Post a Comment