Reputation: 6649
Let say I want to test my controller behavior, but it accepts JSON string via GET.
Right now I have var in my test class @testJson, but from time to time some unexpected stuff happens to those JSONS (bad char inside i.e.). So I want to add another test case.
But adding another var @problematicJson1 (and there could be more probably) doesn't seems like a good idea.
What's is the best way to keep "fixtures" like that? Should I keep'em in files and load them? Is there some fixture feature i don't know about that could help?
Upvotes: 0
Views: 128
Reputation: 16435
Those things are not fixtures.
You should use a neat feature of RSpec (if you are using RSpec at all) that allows to lazily define variables, so the actual variable is instantiated only when used by a specific "it", even if it is defined in an outer "context/describe" block.
https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/helper-methods/let-and-let
context "some context" do
let(:testJson) { put your json inside the block }
let(:otherJson) { {:my_json => textJson} } # this will use the defined testJson
it "something" do
testJson.should have_key "blah"
end
context "some internal context"
let(:testJson) { something else }
it "some other test" do
otherJson[:my_json].should ....
# this will use the local version of testJson
# you only have to redefine the things you need to, unlike a before block
end
end
end
Upvotes: 1