rspec - 区分Rspec的 'let'与 'let!'的问题

标签 rspec let

我已经阅读了rspec docs并搜索了许多其他地方,但是我很难把握Rspec的letlet!之间的区别

我已经读过let直到需要它才被初始化,并且它的值仅在每个示例中被缓存。我还读过let!强制变量立即存在,并为每个示例强制调用。我想是因为我是新手,所以很难看到它与以下示例的关系。为什么需要使用:m1设置let!来断言页面上存在m1.content,但是可以使用:user设置let来断言页面包含text: user.name

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
    let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }

    before { visit user_path(user) }

    it { should have_selector('h1',    text: user.name) }
    it { should have_selector('title', text: user.name) }

    describe "microposts" do
      it { should have_content(m1.content) }
      it { should have_content(m2.content) }
      it { should have_content(user.microposts.count) }
    end
  end

  describe "after saving the user" do
    before { click_button submit }
    let(:user) { User.find_by_email('user@example.com') }

    it { should have_selector('title', text: user.name) }
    it { should have_success_message('Welcome') } 
    it { should have_link('Sign out') }
  end

最佳答案

因为before块正在调用visit user_path(user),所以用户值在那里被初始化,并且RSpec将访问该页面。如果:m1 :m2未使用let!,则访问将不会产生任何内容

it { should have_content(m1.content) }
it { should have_content(m2.content) }

失败,因为它希望在用户访问页面之前创建微信息。 let!允许在调用before块之前创建微博,并且当测试访问页面时,应该已经创建了微博。

编写相同测试并使它们通过的另一种方法是执行以下操作:
describe "profile page" do
  let(:user) { FactoryGirl.create(:user) }
  let(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
  let(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }

  before do
    m1
    m2
    visit user_path(user)
  end

m1之前调用变量m2visit user_path(user)会导致它们在访问页面之前被初始化,并导致测试通过。

更新
这个小例子更有意义:

在此示例中,我们调用get_all_posts,它仅返回帖子数组。注意,我们在断言之前和it块执行之前调用该方法。因为post在执行断言之前不会被调用。
def get_all_posts
  Post.all
end

let(:post) { create(:post) }

before { @response = get_all_posts }

it 'gets all posts' do 
  @response.should include(post)
end

通过使用let!,该帖子将在RSpec看到该方法后立即创建(在before块之前),并且该帖子将在Post列表中返回

同样,另一种方法是在调用方法之前,在before块中调用变量名。
before do
  post
  @response = get_all_posts
end

这样可以确保在调用方法本身创建let(:post)之前先调用Post块,以便在Post.all调用中将其返回

关于rspec - 区分Rspec的 'let'与 'let!'的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17407733/

相关文章:

ruby-on-rails - 如何使用 RSpec 正确测试 CanCan 能力

javascript - 如何使用 let 声明作为表达式?

ruby-on-rails-3 - 如何使用 rspec-rails 测试 Controller ?

ruby-on-rails - Rspec NameError,如何重构这段代码并正确使用路由助手?

ruby-on-rails - 如何使用 rspec 正确测试 ActiveJob 的 retry_on 方法?

javascript - Safari 是否存在 ES6 关键字 'let' 的问题?

Swift - 以编程方式创建 View 时的 Lazy Var 与 Let(节省内存)

javascript - 脚本范围的目的是什么?

lisp - 在 LISP 中, "let"和 "with"有什么区别?

ruby - 使用 RSpec 模拟 TCPSocket