How do I write a spec to test whether an object has a specific value?
I'd like 开发者_运维问答to test whether an object has a certain value on a Rails action. How can I do that with Rspec?
My initial attempt at it was:
it "should have @body_class equal to 'buildings'" do
response.should =~ / buildings /
end
EDIT:
I should specify that @body_class is being set in the controller, and used by a helper to assign a value to the class attribute of the body tag. I expected it might be available in the response object, but it isn't.
Instance variables can be found in the assigns
hash:
# controller
def index
@foo = "foo"
end
# spec
it "should assign foo" do
get :index
assigns[:foo].should == "foo"
end
精彩评论