简体   繁体   中英

How do I define a variable in a RSpec feature?

Im learning Rails 4 and and trying to write some tests using Rspec and capybara. Im writing a feature test for my users and I'm trying to test a user signing in.

feature "User" do
        scenario "A user signs in" do
            let(:user) { FactoryGirl.create(:user) }
            visit signin_path

            fill_in "Username", with: user.username
            fill_in "Password", with: "123456"
            click_button "Log in"

            expect(page).to have_content(user.username)
        end
end

Its telling me that let is an undefined method. and I'm sure that the problem is that it is in a feature/scenario test. How do I define the user in this kind of test?

Also, in a describe/it request test like this

describe "Something" do
    it "should do something" do
        expect(page).to have_content{"...")
    end
end

I can shorten it like this

describe "Something" do
    subject { page }
    it { should have_content("...") }
end

Is there a way to shorten the expect(page)..... in a scenario? Thanks.

let is used for lazy-initialization of "variables" as needed across multiple tests; initializing it in a test is nonsensical. Either move the let outside of the scenario block, or just use standard variable assignment, like user = FactoryGirl.create(:user) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM