Używanie ukrytego "subject" z "expect" w RSpec-2.11
Z nową składnią expect
w rspec-2.11, jak jest możliwe użycie implicit subject
? Czy jest lepszy sposób niż wyraźne odniesienie subject
, Jak Poniżej?
describe User do
it 'is valid' do
expect(subject).to be_valid # <<< can `subject` be implicit?
end
end
42
3 answers
Jeśli skonfigurujesz RSpec, aby wyłączyć składnię should
, nadal możesz używać starej składni jednowierszowej, ponieważ nie wymaga to dodawania should
do każdego obiektu:
describe User do
it { should be_valid }
end
Krótko omówiliśmy alternatywną składnię jednowierszową, ale zdecydowaliśmy się ją odrzucić, ponieważ nie była potrzebna i czuliśmy, że może to spowodować zamieszanie. Możesz jednak łatwo dodać to samemu, jeśli wolisz, jak to brzmi:
RSpec.configure do |c|
c.alias_example_to :expect_it
end
RSpec::Core::MemoizedHelpers.module_eval do
alias to should
alias to_not should_not
end
Mając to na swoim miejscu, możesz napisać to jako:
describe User do
expect_it { to be_valid }
end
64
Author: Myron Marston,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-25 23:55:08
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-25 23:55:08
Z Rspec 3.0 możesz użyć is_expected
zgodnie z opisem TUTAJ .
describe Array do
describe "when first created" do
# Rather than:
# it "should be empty" do
# subject.should be_empty
# end
it { should be_empty }
# or
it { is_expected.to be_empty }
end
end
17
Author: Javid Jamae,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-20 19:53:32
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-20 19:53:32
Można użyć nowej składni nazwanego obiektu, chociaż nie jest to niejawne.
describe User do
subject(:author) { User.new }
it 'is valid' do
expect(author).to be_valid
end
end
12
Author: Hosam Aly,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-04 11:00:31
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-04 11:00:31