Skip to content

Instantly share code, notes, and snippets.

@ms-tg
Created November 10, 2011 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ms-tg/1355523 to your computer and use it in GitHub Desktop.
Save ms-tg/1355523 to your computer and use it in GitHub Desktop.
lightning talk
How do we test TIM controllers from the html parameters?
Rails:
test "should create post" do
assert_difference('Post.count') do
post :create, :post => { :title => 'Hi', :body => 'This is my first post.'}
end
assert_redirected_to post_path(assigns(:post))
assert_equal 'Post was successfully created.', flash[:notice]
end
Notice that the post method in the rails test helpers directly can directly choose
an action, and provide a map of post variables in a similar format to how the
controller code will process it.
What is this most like in a TIM context?
params => HTMLParameters
action => ThreeStageController @DefaultAction OR specific action
referenced in HTMLParameters
Explanation: the TIM, unlike Rails, is not RESTful in action dispatch.
It always hits the /monitor url endpoint, with parameters to identify the controller,
and an action if not the default action. How these work in the TIM is an interesting
topic that we've had to come up to speed on, but is beyond the scope of this
lightning talk.
Can we write TIM controller tests that construct the HTMLParameters in a natural way,
to test in from there a la Rails? Certainly, you can use
TIMPersistantFrame#createDocumentFromInjectedController(...), for example,
passing in an HTMLParameters object.
Document content = createDocumentFromInjectedController(
controller, params, new ScreenMessages());
In a number of TIM tests, this is sufficient, as the format of the
parameters is simple. Sweet!
BUT...
What if our controller makes use of DataSets for CRUD operations on data...
like... most of TIM's significant controllers?
1. construct DataSets manually using pure DataSets (examples)
2. construct DataSets using test-specific builders layered
on controller-specific DataSets (examples)
...
3. Profit?
No way to go from DataSet to HTMLParameters!
To solve this, we wrote DataSetHTMLParams.
Call static method constructHTMLParameters(String action, DataSet ds), for
example in ManageMacroInstrumentsControllerIntegrationTest:
@Test public void
can_delete_a_macro_asset_type() throws Exception {
MacroAssetType commodities = givenRandomMacroAssetType("Commodities", false, null);
MacroInstrumentsPage macroInstrumentsPage =
loadMacroInstrumentsPage(paramsToDeleteAssetTypes(asList(commodities)));
macroInstrumentsPage.assertMacroAssetTypeIsNotPresent(commodities);
}
...
private HTMLParameters paramsToDeleteAssetTypes(List<MacroAssetType> assetTypes) {
return constructHTMLParameters(
"SAVE_MACRO_REGIONS_AND_ASSET_TYPES",
createMacroAssetTypeDataSetContainingDeletedRows(
availableAssetTypes, assetTypes));
}
Questions?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment