问题
I have been struggling to using ruby/rspec/capybara/devise to test my code. A simple test I am trying to write is for signing in. I have a valid user sign in and expect to see an h1 tag as defined in the following code:
describe "Authentication" do
subject { page }
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in_with user.email }
it { should have_css('h1', text: "Welcome to the Test") }
end
end
Problem is that I get this in return:
1) Authentication signin page with valid information
Failure/Error: it { should have_css('h1', text: "Welcome to the Test") }
expected css "h1" with text "Welcome to the Test" to return something
# ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'
Is there a way to output what the test found in the h1 (or that it didn't find it at all?) instead of it didn't find what it expected? There is a good chance my sign_in method is not working, but I can't validate that because I'm not sure what the test sees after sign_in_with executes.
Thanks and happy to provide more context if it's helpful.
EDIT Updated code to reflect subject of tests.
回答1:
... I'm not sure what the test sees after sign_in_with executes.
You can open a snapshot of the current page with save_and_open_page:
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in_with user.email }
it { save_and_open_page; should have_css('h1', text: "Welcome to the Test") }
end
回答2:
There is no subject, you can't use it
to represent result. For Capybrara, you need to check the page
object returned.
Let's rewrite the test like this:
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in_with user.email
end
it "signs in successfully" do
expect(page).to have_css('h1', text: "Welcome to the Test")
end
end
Or better, with Capybara story DSL
feature "User sign in" do
given(:user) { FactoryGirl.create(:user) }
scenario "with valid information " do
sign_in_with user.email
expect(page).to have_css('h1', text: "Welcome to the Test")
end
scenario "with invalid filing" do
sign_in_with "foo@bar.com"
expect(page).to have_text("Invalid email or password")
end
end
来源:https://stackoverflow.com/questions/15873016/how-do-you-get-rspec-to-output-what-it-encountered-rather-than-it-didnt-find-w