ruby on rails - how to use the context in the tests? -
please write tests context.
model album:
class album < activerecord::base validates :title, presence: true, length: { maximum: 50, minimum: 3 }, uniqueness: { case_sensitive: false } validates :description, presence: true, length: { maximum: 600, minimum: 10 } end
i wrote tests model album:
describe album "has valid factory" expect(factorygirl.create(:album)).to be_valid end "is invalid without title" expect(factorygirl.build(:album, title: nil)).not_to be_valid end "is invalid duplicate title" factorygirl.create(:album, title: 'qwerty') expect(factorygirl.build(:album, title: 'qwerty')).not_to be_valid end "is valid different title" factorygirl.create(:album, title: 'zxcvbn') expect(factorygirl.build(:album, title: 'asdfgh')).to be_valid end end
these tests worked ok. need use context:
describe album "has valid factory" expect(factorygirl.create(:album)).to be_valid end describe '#title' context "invalid" "is invalid without title" expect(factorygirl.build(:album, title: nil)).not_to be_valid end "is invalid long title" expect(factorygirl.build(:album, title: 'if liked series on practical advice adding reliable tests rails apps, check out expanded ebook version. lots of additional, exclusive content , complete sample rails application.')).not_to be_valid end "is invalid duplicate title" factorygirl.create(:album, title: 'qwerty') expect(factorygirl.build(:album, title: 'qwerty')).not_to be_valid end end context "valid" "is valid title" expect(factorygirl.build(:album, title: 'good title')).not_to be_valid end "is valid different title" factorygirl.create(:album, title: 'zxcvbn') expect(factorygirl.build(:album, title: 'asdfgh')).to be_valid end end end end
but these tests not dry. please write test context again.
ps: practices tried use:
- checking limiting cases (a small value, great value, average value)
- use contexts organization code
- each test should in separate method
your tests using context
looks okay. but, can write better tests using context
following best practices. see better specs guidelines see how write better rspec tests.
also, see following classic example of using context
the rspec style guide
# classic example use of contexts in controller spec creation or update when object saves or not. describe articlescontroller let(:article) { mock_model(article) } describe 'post create' before { article.stub(:new).and_return(article) } 'creates new article given attributes' article.should_receive(:new).with(title: 'the new article title').and_return(article) post :create, article: { title: 'the new article title' } end 'saves article' article.should_receive(:save) post :create end context 'when article saves successfully' before { article.stub(:save).and_return(true) } 'sets flash[:notice] message' post :create flash[:notice].should eq('the article saved successfully.') end 'redirects articles index' post :create response.should redirect_to(action: 'index') end end context 'when article fails save' before { article.stub(:save).and_return(false) } 'assigns @article' post :create assigns[:article].should be_eql(article) end 're-renders "new" template' post :create response.should render_template('new') end end end end
Comments
Post a Comment