Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在测试中使用上下文?_Ruby On Rails_Ruby On Rails 4_Rspec_Rspec2 - Fatal编程技术网

Ruby on rails 如何在测试中使用上下文?

Ruby on rails 如何在测试中使用上下文?,ruby-on-rails,ruby-on-rails-4,rspec,rspec2,Ruby On Rails,Ruby On Rails 4,Rspec,Rspec2,请使用上下文帮助编写测试 模型相册: 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 } en

请使用上下文帮助编写测试

模型相册:

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
describe Album do
  it "has a valid factory" do
    expect(FactoryGirl.create(:album)).to be_valid
  end

  it "is invalid without title" do
    expect(FactoryGirl.build(:album, title: nil)).not_to be_valid
  end  

  it "is invalid with duplicate title" do
    FactoryGirl.create(:album, title: 'qwerty')
    expect(FactoryGirl.build(:album, title: 'qwerty')).not_to be_valid
  end 

  it "is valid with different title" do
    FactoryGirl.create(:album, title: 'zxcvbn')
    expect(FactoryGirl.build(:album, title: 'asdfgh')).to be_valid
  end        
end
这些测试工作正常。但我需要使用上下文:

describe Album do
  it "has a valid factory" do
    expect(FactoryGirl.create(:album)).to be_valid
  end

  describe '#title' do
    context "invalid" do
      it "is invalid without title" do
        expect(FactoryGirl.build(:album, title: nil)).not_to be_valid
      end  

      it "is invalid with long title" do
        expect(FactoryGirl.build(:album, title: 'If you liked my series on practical advice for adding reliable tests to your Rails apps, check out the expanded ebook version. Lots of additional, exclusive content and a complete sample Rails application.')).not_to be_valid
      end        

      it "is invalid with duplicate title" do
        FactoryGirl.create(:album, title: 'qwerty')
        expect(FactoryGirl.build(:album, title: 'qwerty')).not_to be_valid
      end       
    end

    context "valid" do
      it "is valid with title" do
        expect(FactoryGirl.build(:album, title: 'good title')).not_to be_valid
      end  

      it "is valid with different title" do
        FactoryGirl.create(:album, title: 'zxcvbn')
        expect(FactoryGirl.build(:album, title: 'asdfgh')).to be_valid
      end  
    end
  end      
end
但这些测试并不干燥。请再次使用上下文帮助编写测试

附言: 我尝试使用的良好实践:

  • 检查极限情况(非常小的值,非常大的值, 平均值)
  • 使用组织代码的上下文
  • 每个测试应采用单独的方法

  • 您使用
    context
    进行的测试看起来正常。但是,通过遵循最佳实践,您可以使用
    context
    编写更好的测试。请参阅指南,了解如何编写更好的RSpec测试

    另外,请参见以下使用
    上下文的经典示例


    但这个例子是针对Controlleries的,但它向您展示了如何在RSpec测试中正确使用
    context
    。我建议您看看这个:这将帮助您在适当的上下文中编写更好的规范测试。
    # A classic example for use of contexts in a controller spec is creation or update when the object saves successfully or not.
    
    describe ArticlesController do
      let(:article) { mock_model(Article) }
    
      describe 'POST create' do
        before { Article.stub(:new).and_return(article) }
    
        it 'creates a new article with the given attributes' do
          Article.should_receive(:new).with(title: 'The New Article Title').and_return(article)
          post :create, article: { title: 'The New Article Title' }
        end
    
        it 'saves the article' do
          article.should_receive(:save)
          post :create
        end
    
        context 'when the article saves successfully' do
          before { article.stub(:save).and_return(true) }
    
          it 'sets a flash[:notice] message' do
            post :create
            flash[:notice].should eq('The article was saved successfully.')
          end
    
          it 'redirects to the Articles index' do
            post :create
            response.should redirect_to(action: 'index')
          end
        end
    
        context 'when the article fails to save' do
          before { article.stub(:save).and_return(false) }
    
          it 'assigns @article' do
            post :create
            assigns[:article].should be_eql(article)
          end
    
          it 're-renders the "new" template' do
            post :create
            response.should render_template('new')
          end
        end
      end
    end