无效的参数异常-Laravel单元测试

时间:2019-05-09 12:31:41

标签: laravel phpunit factory

我正在运行单元测试以检查

  1. 查看页面存在
  2. 确认看到该文本显示在页面上并且具有字符串限制

我收到无效的参数异常:

class HomePage extends Component {
  state = {
    topTv: [],
    topAiring: []
  }

  async getData() {
    const api = "https://api.jikan.moe/v3"
    const urls = [
      `${api}/top/anime/1/tv`,
      `${api}/top/anime/1/airing`
    ];

    return Promise.all(
      urls.map(async url => {
        return await fetch(url) // fetch data from urls
      })
    )
      .then(responses => // convert response to json and setState to retrieved data
        Promise.all(responses.map(resp => resp.json())).then(data => {
          console.log("data", data)
          // const results = [...data[0].top, ...data[1].top]; // data from TV & data from airing
          const reduceResults = data.reduce((acc, anime) => {
            return acc + anime
          }, [])
          console.log('reduce', reduceResults);

          const tvResults = data[0].top // data from TV
          const airingResults = data[1].top // data from airing
          this.setState({
            topTv: tvResults,
            topAiring: airingResults
          });
        })
      )
      .catch(err => console.log("There was an error:" + err))
  }

  componentDidMount() {
    this.getData();
  }

  render() {
    return (
      <HomeWrapper>
        <h2>Top anime</h2>
        <TopAnime>
          {this.state.topTv.map((ani) => {
            return (
              <div key={ani.mal_id}>
                <img src={ani.image_url} alt='anime poster' />
                <h3>{ani.title}</h3>
              </div>
            )
          }).splice(0, 6)}
        </TopAnime>
        <h2>Top Airing</h2>
        <TopAnime>
          {this.state.topAiring.map((ani) => {
            return (
              <div key={ani.mal_id}>
                <img src={ani.image_url} alt='anime poster' />
                <h3>{ani.title}</h3>
              </div>
            )
          }).splice(0, 6)}
        </TopAnime>
      </HomeWrapper>
    )
  }
}

我的源代码如下:

测试功能

jwt_id

工厂类

1) Tests\Feature\ViewAllPostTest::testCanViewAllPosts
InvalidArgumentException: You requested 1 items, but there are only 0 items available.

C:\projects\car-torque-laravel\vendor\laravel\framework\src\Illuminate\Support\Arr.php:472
C:\projects\car-torque-laravel\vendor\laravel\framework\src\Illuminate\Support\Collection.php:1486
C:\projects\car-torque-laravel\database\factories\PostFactory.php:12
C:\projects\car-torque-laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:274
C:\projects\car-torque-laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:292
C:\projects\car-torque-laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php:122
C:\projects\car-torque-laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:300
C:\projects\car-torque-laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:219
C:\projects\car-torque-laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:178
C:\projects\car-torque-laravel\tests\Feature\ViewAllPostTest.php:19

1 个答案:

答案 0 :(得分:1)

'user_id' => User::all()->random()->id,

在工厂的上述行中,您希望从用户表中获得随机ID。但是在运行测试之前您是否创建了任何用户。使用post factory创建帖子之前,至少应创建一个用户。