Yet Another Blog

Rambler, Developer and gamer

Using Lambdas With Let (in Rspec)

| Comments

As many of you already know, let has been a great improvement on declaring instance variables in our before :each blocks. Lazy loading on when it’s used while being kept in memory for the duration of the test. One way in which we use lets, for more complicated variable definitions, is to use let, and provide it with arguments using a lambda:

1
2
3
4
5
let(:user) {
    ->(name, email) {
      double("user", user: email, email: email)
    }
  }

You can then easily setup users in your test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
describe 'Given a user' do
  context 'with internal address' do
    it 'redirects to intranet' do
      login_as user['Bob', 'bob@internal.com']
      ...
    end
  end
  context 'with external address' do
    let(:staff_user) { user['Bob', 'bob@internal.com'] }
    it 'redirects to external domain' do
      login_as user['Pete', 'pete@gmail.com']
      ...
    end
  end
end

Comments